Random Technical Thoughts

December 6, 2011

Updated – Search for string across all fields in all tables in a database

Filed under: SQL — Tags: , — chrisbarba @ 2:16 pm

This is an update to a previous post.

It has been updated to use table variables instead of temp tables.
This helps when you are in an environment (like production) where you don’t have permissions to create/drop temp tables.

DECLARE @Results TABLE (ColumnName nvarchar(370),   ColumnValue nvarchar(3630))

DECLARE @SearchStr nvarchar(100)

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)

SET @TableName = ''
SET @SearchStr = 'STRING TO SEARCH FOR'

SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL
BEGIN
SET @ColumnName = '' SET @TableName =
(
SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
AND OBJECTPROPERTY(
OBJECT_ID(
QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
), 'IsMSShipped' ) = 0
)
WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
BEGIN
SET @ColumnName =
(
SELECT MIN(QUOTENAME(COLUMN_NAME))
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
AND TABLE_NAME = PARSENAME(@TableName, 1)
AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
AND QUOTENAME(COLUMN_NAME) > @ColumnName
)

IF @ColumnName IS NOT NULL
BEGIN
INSERT INTO @Results
EXEC
(
'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + ' (NOLOCK) ' +
' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
)
END
END
END
SELECT ColumnName, ColumnValue FROM @Results

April 9, 2011

How to add a content editor web part to a SharePoint page

Filed under: SharePoint, Web Part — Tags: , — chrisbarba @ 10:40 pm

Here is some code to add a content editor web part to a page.
So if you are creating a page in code (like during feature activation), then you put a content editor web part on the page.

Setting the InnerText (contentXMLElement.InnerText = "";) will set the text in the content editor, incase you want something to be there by default.  User HTML.

 

using (SPWeb webSite = SPContext.Current.Site.OpenWeb(SiteToOpen))
{
     using (SPLimitedWebPartManager mgr = webSite.GetFile("default.aspx").GetLimitedWebPartManager(PersonalizationScope.Shared))
     {
          if (mgr != null)
          {
               # region AddNewLink
 
               ContentEditorWebPart cewp = new ContentEditorWebPart();
               cewp.AllowClose = false;
               cewp.AllowEdit = false;
               cewp.AllowHide = false;
               cewp.AllowMinimize = false;
               cewp.ID = "ContentEditorWP";
               cewp.Title = "Content Editor Web Part";
               cewp.ChromeType = PartChromeType.None;
 
               //Add content to the content editor web part
               XmlDocument addNewXMLDoc = new XmlDocument();
               XmlElement contentXMLElement = addNewXMLDoc.CreateElement("Root");
               contentXMLElement.InnerText = "";
               cewp.Content = contentXMLElement;
               cewp.Content.InnerText = contentXMLElement.InnerText;
 
                    
               // add the web part.   
               // first argument: web part object   
               // second argument: zone   
               // third argument: index (location within the zone)   
               mgr.AddWebPart(cewp,"left", 0);
 
               # endregion
 
          }
     }
}        

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Method to strip out special characters from a string.

Filed under: C# — Tags: — chrisbarba @ 10:12 pm

Special characters can cause all kind of trouble. 
So here is a method that you pass in a string and all the special characters will be removed.

 

public static string StripSpecialCharacters(string stringToConvert)
        {
            stringToConvert = stringToConvert.Replace("~", string.Empty);
            stringToConvert = stringToConvert.Replace("#", string.Empty);
            stringToConvert = stringToConvert.Replace("%", string.Empty);
            stringToConvert = stringToConvert.Replace("&", string.Empty);
            stringToConvert = stringToConvert.Replace("*", string.Empty);
            stringToConvert = stringToConvert.Replace("{", string.Empty);
            stringToConvert = stringToConvert.Replace("}", string.Empty);
            stringToConvert = stringToConvert.Replace("\\", string.Empty);
            stringToConvert = stringToConvert.Replace(":", string.Empty);
            stringToConvert = stringToConvert.Replace("<", string.Empty);
            stringToConvert = stringToConvert.Replace(">", string.Empty);
            stringToConvert = stringToConvert.Replace("?", string.Empty);
            stringToConvert = stringToConvert.Replace("/", string.Empty);
            stringToConvert = stringToConvert.Replace("|", string.Empty);
            stringToConvert = stringToConvert.Replace("\"", string.Empty);
 
            return stringToConvert;
        } 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

How to create sub sites in SharePoint 2007

Filed under: SharePoint — Tags: — chrisbarba @ 10:07 pm

Here is some code to create a subsite in SharePoint.
The important part to remember is to set AllUnsafeUpdates (and unset), otherwise it won’t work.

try
{
    SPWeb webSite = SPContext.Current.Web;

    webSite.AllowUnsafeUpdates = true;

        //Add subsite site
        webSite.Webs.Add(<WEBURL>, <TITLE>, <DESCRIPTION>, LOCALE_ID_ENGLISH, <SITE TEMPLATE>, true, false);

    webSite.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
    throw ex;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

February 4, 2011

Detect who the current user in a SharePoint web part

Filed under: SharePoint, Web Part — Tags: , — chrisbarba @ 11:12 pm

It’s easy to detect who the current authenticated in ASP.HET (HttpContext.Current.User), but that doesn’t work in a web part.
So in a web part, you need to open a web and then you access the current user.

Below is an example (you just have to specify the “SiteToOpen”).

 

using (SPWeb webSite = SPContext.Current.Site.OpenWeb(SiteToOpen))
{
     //Determine who the current user is
        String currentUser = webSite.CurrentUser.Name;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

February 2, 2011

How to cache data in a SharePoint web part

Filed under: SharePoint, Web Part — Tags: , — chrisbarba @ 12:09 am

Here is some sample code to cache a list of strings in a web part.

List<String> documentNames = new List<String>();
 
 object cacheddocumentNames = PartCacheRead(Storage.Shared, "DocumentNamesList");
 if (cacheddocumentNames != null)
 {
       // cache exists, use it  
        documentNames = (List<String>)cacheddocumentNames as List<String>;
 }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

February 1, 2011

Programmatically set url field on list

Filed under: SharePoint — Tags: — chrisbarba @ 11:58 pm

I had to find a way to programmatically add items to list that has a url field.
Here is an example of how to do it.
When you set the field that is an URL you have specify a url a coma (and a space) and the value to display.

 

using (SPWeb webSite = SPContext.Current.Site.OpenWeb(SiteToOpen))
{
       SPListCollection lists = webSite.Lists;
 
       SPList theLlist = webSite.Lists["listName"];
 
       string documentUrl = String.Empty;
 
       foreach (DocumentItems d in documents)
       {
            SPListItem listItem = theLlist.Items.Add();
 
             listItem["Title"] = "ItemTitle";
             listItem["Link"] = "URL" + ", " + "Display Value";
 
             listItem.Update();
        }
 }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Create a SharePoint list of the States

Filed under: SharePoint — Tags: — chrisbarba @ 9:43 am

Here is a method that will create a list of States in the US.
It will create the list in the current site.

 

private void CreateUSStateList()
        {
            SPContext.Current.Web.AllowUnsafeUpdates = true;
 
            SPWeb mySite = SPContext.Current.Web;
 
            SPListCollection lists = mySite.Lists;
            lists.Add(USStateList, "List of US States", SPListTemplateType.GenericList);
 
            SPList usStateList = mySite.Lists[USStateList];
 
            # region States
 
            //  This is a list of the United States and its Territories in alphabetical order: 
            String[] stateArray = {
                                    "Alabama"
                                    ,"Alaska"
                                    ,"American Samoa"
                                    ,"Arizona"
                                    ,"Arkansas"
                                    ,"California"
                                    ,"Colorado"
                                    ,"Connecticut"
                                    ,"Delaware"
                                    ,"District of Columbia"
                                    ,"Florida"
                                    ,"Georgia"
                                    ,"Guam"
                                    ,"Hawaii"
                                    ,"Idaho"
                                    ,"Illinois"
                                    ,"Indiana"
                                    ,"Iowa"
                                    ,"Kansas"
                                    ,"Kentucky"
                                    ,"Louisiana"
                                    ,"Maine"
                                    ,"Maryland"
                                    ,"Massachusetts"
                                    ,"Michigan"
                                    ,"Minnesota"
                                    ,"Mississippi"
                                    ,"Missouri"
                                    ,"Montana"
                                    ,"Nebraska"
                                    ,"Nevada"
                                    ,"New Hampshire"
                                    ,"New Jersey"
                                    ,"New Mexico"
                                    ,"New York"
                                    ,"North Carolina"
                                    ,"North Dakota"
                                    ,"Northern Marianas Islands "
                                    ,"Ohio"
                                    ,"Oklahoma"
                                    ,"Oregon"
                                    ,"Pennsylvania"
                                    ,"Puerto Rico"
                                    ,"Rhode Island"
                                    ,"South Carolina"
                                    ,"South Dakota"
                                    ,"Tennessee"
                                    ,"Texas"
                                    ,"Utah"
                                    ,"Vermont"
                                    ,"Virginia"
                                    ,"Virgin Islands"
                                    ,"Washington"
                                    ,"West Virginia"
                                    ,"Wisconsin"
                                    ,"Wyoming"
                                   };
 
            # endregion
 
            # region Add items
 
            foreach (String state in stateArray)
            {
                SPListItem listItem = usStateList.Items.Add();
                listItem["Title"] = state;
                listItem.Update();
            }
      
            # endregion
 
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

January 31, 2011

Create a list of Countries for SharePoint

Filed under: SharePoint — Tags: — chrisbarba @ 4:19 pm

Here is a method to create a list and populate the list with the countries of the world.

 

 

private void CreateCountryList()
       {
           SPContext.Current.Web.AllowUnsafeUpdates = true;
 
           SPWeb mySite = SPContext.Current.Web;
 
           SPListCollection lists = mySite.Lists;
           lists.Add(CountryList, "List of Countries", SPListTemplateType.GenericList);
 
           SPList countryList = mySite.Lists[CountryList];
 
           # region Countries
 
           //  This is a list of the United States and its Territories in alphabetical order: 
           String[] countryArray = {
                                   "United States"
                                   ,"Afghanistan"
                                   ,"Algeria"
                                   ,"Andorra" 
                                   ,"Angola" 
                                   ,"Antigua and Barbuda" 
                                   ,"Argentina"
                                   ,"Armenia"
                                   ,"Australia" 
                                   ,"Austria"
                                   ,"Azerbaijan"
                                   ,"Bahamas, The"
                                   ,"Bahrain"
                                   ,"Bangladesh"
                                   ,"Barbados" 
                                   ,"Belarus" 
                                   ,"Belgium" 
                                   ,"Belize" 
                                   ,"Benin" 
                                   ,"Bhutan" 
                                   ,"Bolivia" 
                                   ,"Bosnia and Herzegovina" 
                                   ,"Botswana" 
                                   ,"Brazil" 
                                   ,"Brunei"  
                                   ,"Bulgaria" 
                                   ,"Burkina Faso"
                                   ,"Burma"
                                   ,"Burundi"
                                   ,"Cambodia"
                                   ,"Cameroon" 
                                   ,"Canada "
                                   ,"Cape Verde "
                                   ,"Central African Republic" 
                                   ,"Chad "
                                   ,"Chile "
                                   ,"China "
                                   ,"Colombia "
                                   ,"Comoros "
                                   ,"Congo (Brazzaville)" 
                                   ,"Congo (Kinshasa) "
                                   ,"Costa Rica "
                                   ,"Cote d'Ivoire "
                                   ,"Croatia "
                                   ,"Cuba "
                                   ,"Cyprus "
                                   ,"Czech Republic"
                                   ,"Denmark "
                                   ,"Djibouti "
                                   ,"Dominica "
                                   ,"Dominican Republic"
                                   ,"East Timor"
                                   ,"Ecuador "
                                   ,"Egypt "
                                   ,"El Salvador "
                                   ,"Equatorial Guinea "
                                   ,"Eritrea "
                                   ,"Estonia "
                                   ,"Ethiopia"
                                   ,"Fiji "
                                   ,"Finland" 
                                   ,"France"
                                   ,"Gabon"
                                   ,"Gambia, The"
                                   ,"Georgia"
                                   ,"Germany"
                                   ,"Ghana"
                                   ,"Greece"
                                   ,"Grenada"
                                   ,"Guatemala"
                                   ,"Guinea"
                                   ,"Guinea-Bissau" 
                                   ,"Guyana"
                                   ,"Haiti"
                                   ,"Holy See" 
                                   ,"Honduras"
                                   ,"Hong Kong" 
                                   ,"Hungary"
                                   ,"Iceland"
                                   ,"India"
                                   ,"Indonesia" 
                                   ,"Iran"
                                   ,"Iraq"
                                   ,"Ireland" 
                                   ,"Israel"
                                   ,"Italy"
                                   ,"Jamaica"
                                   ,"Japan"
                                   ,"Jordan"
                                   ,"Kazakhstan" 
                                   ,"Kenya "
                                   ,"Kiribati" 
                                   ,"Korea, North" 
                                   ,"Korea, South"
                                   ,"Kosovo"
                                   ,"Kuwait"
                                   ,"Kyrgyzstan"
                                   ,"Laos" 
                                   ,"Latvia" 
                                   ,"Lebanon" 
                                   ,"Lesotho"
                                   ,"Liberia" 
                                   ,"Libya"
                                   ,"Liechtenstein" 
                                   ,"Lithuania" 
                                   ,"Luxembourg"
                                   ,"Macau"
                                   ,"Macedonia"
                                   ,"Madagascar" 
                                   ,"Malawi"
                                   ,"Malaysia"
                                   ,"Maldives" 
                                   ,"Mali"
                                   ,"Malta"
                                   ,"Marshall Islands"
                                   ,"Mauritania"
                                   ,"Mauritius"
                                   ,"Mexico"
                                   ,"Micronesia" 
                                   ,"Moldova" 
                                   ,"Monaco" 
                                   ,"Mongolia" 
                                   ,"Montenegro"
                                   ,"Morocco" 
                                   ,"Mozambique"
                                   ,"Namibia" 
                                   ,"Nauru" 
                                   ,"Nepal" 
                                   ,"Netherlands" 
                                   ,"Netherlands Antilles"
                                   ,"New Zealand"
                                   ,"Nicaragua"
                                   ,"Niger"
                                   ,"Nigeria"
                                   ,"North Korea"
                                   ,"Norway"
                                   ,"Oman"
                                   ,"Pakistan"
                                   ,"Palau"
                                   ,"Palestinian Territories" 
                                   ,"Panama" 
                                   ,"Papua New Guinea" 
                                   ,"Paraguay" 
                                   ,"Peru" 
                                   ,"Philippines" 
                                   ,"Poland" 
                                   ,"Portugal"
                                   ,"Qatar" 
                                   ,"Romania"
                                   ,"Russia" 
                                   ,"Rwanda"
                                   ,"Saint Kitts and Nevis" 
                                   ,"Saint Lucia" 
                                   ,"Saint Vincent and the Grenadines" 
                                   ,"Samoa" 
                                   ,"San Marino" 
                                   ,"Sao Tome and Principe" 
                                   ,"Saudi Arabia" 
                                   ,"Senegal" 
                                   ,"Serbia" 
                                   ,"Seychelles" 
                                   ,"Sierra Leone" 
                                   ,"Singapore" 
                                   ,"Slovakia" 
                                   ,"Slovenia" 
                                   ,"Solomon Islands" 
                                   ,"Somalia" 
                                   ,"South Africa" 
                                   ,"South Korea" 
                                   ,"Spain" 
                                   ,"Sri Lanka" 
                                   ,"Sudan" 
                                   ,"Suriname" 
                                   ,"Swaziland" 
                                   ,"Sweden" 
                                   ,"Switzerland" 
                                   ,"Syria"
                                   ,"Taiwan" 
                                   ,"Tajikistan"
                                   ,"Tanzania" 
                                   ,"Thailand" 
                                   ,"Timor-Leste" 
                                   ,"Togo" 
                                   ,"Tonga" 
                                   ,"Trinidad and Tobago" 
                                   ,"Tunisia" 
                                   ,"Turkey" 
                                   ,"Turkmenistan" 
                                   ,"Tuvalu"
                                   ,"Uganda"
                                   ,"Ukraine" 
                                   ,"United Arab Emirates" 
                                   ,"United Kingdom" 
                                   ,"Uruguay" 
                                   ,"Uzbekistan"
                                   ,"Vanuatu" 
                                   ,"Venezuela" 
                                   ,"Vietnam"
                                   ,"Yemen"
                                   ,"Zambia" 
                                   ,"Zimbabwe" 
                                  };
 
           # endregion
 
           # region Add items
 
           foreach (String country in countryArray)
           {
               SPListItem listItem = countryList.Items.Add();
               listItem["Title"] = country;
               listItem.Update();
           }
 
           # endregion
 
 
 
       }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

How to add a required field validator to a SharePoint web part.

Filed under: SharePoint, WebPart — Tags: — chrisbarba @ 4:09 pm

You just have to set the ID property of the control, then set the control to validate to the ID of the control.

Below is an example

//Declare Controls
private TextBox _txtFirstName = new TextBox();
RequiredFieldValidator _rfvFirstName = new RequiredFieldValidator();

//Add text box
this._txtFirstName.ID = "txtFirstName";
this.Controls.Add(_txtFirstName);

//Required field validator
_rfvFirstName.Text = "* First Name is required!";
_rfvFirstName.Display = ValidatorDisplay.Dynamic;
_rfvFirstName.ControlToValidate = _txtFirstName.ID;
_rfvFirstName.ForeColor = Color.Red;
_rfvFirstName.ValidationGroup = "adduser";
this.Controls.Add(_rfvFirstName);

Older Posts »

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.