How to create sub sites in SharePoint 2007

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; }

Advertisements

Detect who the current user in a SharePoint web part

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; }

 

Programmatically set url field on list

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

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; }

[tweetmeme only_single=”false”]

Create a list of Countries for SharePoint

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; }

[tweetmeme only_single=”false”]

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

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);

[tweetmeme only_single=”false”]

Check if a SharePoint list exists

SharePoint 2007 doesn’t have any methods to tell you if a list exists.
So you have to try to get an instance of the list and catch the error.

So here the method I use to check if a list exist.
You pass in the SPListCollection and the title of the lists.

//Check if a list already exists
private static bool listExists(SPListCollection collection, string title)
{
try
{
foreach (SPList list in collection)
{
if (list.Title == title)
{
return true;
}
}
return false;
}
catch (Exception)
{

throw;
}
}

SharePoint filename special characters not allowed

I was creating files in memory and loading them into a doc library based on user input on a webpart.
Some smart user put special characters in and of course it blew up my web part.

So I had to research what characters are not allowed in file names.

Here they are:
~, #, %, & , *, {, }, \, :, <, >, ?, /, |, “

So I wrote a little function to remove the special characters.

public static string RemoveSpecialCharacters(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);
}

So any filename I was going to use got run through this function to clean it up.

 

[tweetmeme only_single=”false”]

Do’s and Don’ts on SharePoint

Once you get into using SharePoint, you get an idea of how big it is.
At that point you start to come across some questions of what’s the best way to do something.

I found a great article of some lessons learned about SharePoint.
http://weblogs.asp.net/bsimser/archive/2011/01/12/do-s-and-don-ts-building-sharepoint-applications.aspx

These aren’t best practices, but more along the lines of tips.

The only tip I might add, is not to think before you create a new site.   Only create a new site if you need all of it.
I have seen many times people create multiple sites, when they only need a couple of lists/libraries.
So ask yourself about if you are really going to need a whole site for what your doing.