Understanding SharePoint Property Bag Settings

30 August, 2010 (16:14) | MOSS - Object Model | By: G Vijai Kumar

Today my ex-coworker and friend ask me regarding SharePoint property bag, therefore I got an opportunity of posting this article on understanding SharePoint property bag settings.

SharePoint property bag is a good place where we are able to accumulate key and value like a couple, It’s a hash table.

There is no such user interface to view/add/edit/delete the property bag key-value, but there is an option to perform the actions on property bag via SharePoint designer.

After you open a site in SharePoint Designer, go to Site menu click on Settings option, a Site Settings dialog box opens, click on Parameters tab where you can see the list of existing properties, from the same place you can even perform add/modify/remove actions.

View SharePoint property bag properties using SharePoint Designer

View SharePoint property bag properties using SharePoint Designer

Subsequently, prior to start on deep into property bag, we will discuss going on additional area some where we can store up value other than SharePoint property bag.

web.config file is too a place where we be able to accumulate the settings such as file location, URLs, etc.
Example:

<configuration>
    <appSettings>
        <add key="MyKey" value="MyValue" />
    </appSettings>
</configuration>

In view of the fact that we have single global web.config file meant for complete site. If we would like to accumulate additional settings correlated to several sites we have to make multiple entries in web.config file.

One more method of storing the settings by creating table in database, this approach is not recommend all the time in SharePoint.

Even we can store values in SharePoint list by creating two columns like Keys and Values using OOTB functionality and still can access the key-value pair using object model.

But, if you have several settings related to different sites with some unique permissions for individuals sites then we have to decide where the list is to be created.

On the way to over come this situation SharePoint provided property bag to store the properties in several levels like SPFarm, SPWebApplication, SPSite, SPWeb and SPList.

Storing key-value in SPFarm include more danger that require farm administrator privileges.

Below are few examples on SPFarm, SPWebApplication, SPSite, SPWeb add/view/edit/delete property bag values.

Add property in SPFarm level

static void Main(string[] args)
        {
            SPFarm myFarm = SPFarm.Local;
            myFarm.Properties.Add("SPFarmKey", "SPFarmValue");
            myFarm.Update();
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

View SPFarm property


static void Main(string[] args)
        {
            SSPFarm myFarm = SPFarm.Local;
            if (myFarm.Properties != null && myFarm.Properties.Count > 0)
            {
                if (myFarm.Properties.ContainsKey("SPFarmKey"))
                {
                    Console.WriteLine(myWebApplication.Properties["SPFarmKey"]);
                }
            }
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Modify property in SPFarm level


static void Main(string[] args)
        {
            SPFarm myFarm = SPFarm.Local;
            myFarm.Properties["SPFarmKey"] = "NewSPFarmValue";
            myFarm.Update();
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Delete property in SPFarm level


static void Main(string[] args)
        {
            SPFarm myFarm = SPFarm.Local;
            myFarm.Properties["SPFarmKey"] = null;
            myFarm.Properties.Remove("SPFarmKey");
            myFarm.Update();
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Add property in SPWebApplication level


static void Main(string[] args)
        {
            SPWebApplication myWebApplication = SPWebApplication.Lookup(new Uri("http://WebApplicationURL"));
            myWebApplication.Properties.Add("SPWebAppKey", "SPWebAppValue");
            myWebApplication.Update();
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

View SPWebApplication property


static void Main(string[] args)
        {
            SPWebApplication myWebApplication = SPWebApplication.Lookup(new Uri("http://WebApplicationURL"));
            if (myWebApplication.Properties != null && myWebApplication.Properties.Count > 0)
            {
                if (myWebApplication.Properties.ContainsKey("SPWebAppKey"))
                {
                    Console.WriteLine(myWebApplication.Properties["SPWebAppKey"]);
                }
            }
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Modify property in SPWebApplication level


static void Main(string[] args)
        {
            SPWebApplication myWebApplication = SPWebApplication.Lookup(new Uri("http://WebApplicationURL"));
            myWebApplication.Properties["SPWebAppKey"] = "NewSPWebAppValue";
            myWebApplication.Update();
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Delete property in SPWebApplication level


static void Main(string[] args)
        {
            SPWebApplication myWebApplication = SPWebApplication.Lookup(new Uri("http://WebApplicationURL"));
            myWebApplication.Properties["WebAppKey"] = null;
            myWebApplication.Properties.Remove("WebAppKey");
            myWebApplication.Update();
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

There is no facility to store properties in SPSite object similar to SPWeb object, if you want to store the settings in site collection level we have to use SPSite.RootWeb

Add/retrieve SPSite property


static void Main(string[] args)
        {
            SPSite siteCollection = new SPSite("http://servername:port");
            SPWeb site = siteCollection.RootWeb;
            //Addning SPSite Property
            site.Properties.Add("SPSiteKey", "SPSiteValue");
            site.Properties.Update();

            //Reading SPSite Property
            Console.WriteLine(site.Properties["SPSiteKey"]);
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Add/retrieve SPWeb property

static void Main(string[] args)
        {
            SPSite mySite = new SPSite("http://servername:port");
            SPWeb myWeb = mySite.OpenWeb("Your Web Name.....");
            //Adding SPWeb Property
            myWeb.Properties.Add("SPWebKey", "SPWebValue");
            myWeb.Properties.Update();

            //Reading SPWeb Property
            Console.WriteLine(myWeb.Properties["SPWebKey"]);
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

A quick look on Sharepoint object model programs – Part 3

28 August, 2010 (13:55) | MOSS - Object Model | By: G Vijai Kumar

I have got the chance to post useful Sharepoint object model programs (part 3) with bare minimum lines of code and really handy even.
Please follow the link for a quick look on Sharepoint object model programs Part 1 and Part 2

Creating a Folder in a Document Library:

static void Main(string[] args)
        {
            try
            {
                SPSite mySite = new SPSite("http://servername:port");
                SPWeb myWeb = mySite.OpenWeb();
                SPList MyLibrary = myWeb.Lists["Your Document Library Name....."];
                SPFolder myFolder = MyLibrary.RootFolder;
                SPFolder myChildFolder = myFolder.SubFolders.Add("Your Folder Name.....");
                myFolder.Update();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Add fields to list:

static void Main(string[] args)
        {
            try
            {
                SPSite mySite = new SPSite("http://servername:port");
                SPWeb myWeb = mySite.OpenWeb();
                SPList myList = myWeb.Lists["Your List Name....."];
                Guid listId = myList.ID;
                myList.Fields.Add("Employee ID", SPFieldType.Number, true);
                myList.Fields.Add("Employee Name", SPFieldType.Text, false);
                myList.Fields.Add("Designation", SPFieldType.Text, false);
                myList.Update();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Creating a new view in a list:

static void Main(string[] args)
        {
            try
            {
                SPSite mySite = new SPSite("http://servername:port");
                SPWeb myWeb = mySite.OpenWeb();
                SPList mylist = myWeb.Lists["Your List Name....."];
                StringCollection myFields = new StringCollection();
                myFields.Add("Employee Name");
                myFields.Add("Designation");
                SPView myView= mylist.Views.Add("Your View Name.....", myFields, null, 100, false, false, SPViewCollection.SPViewType.Html, false);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Creating a new Content Type:

static void Main(string[] args)
        {
            try
            {
                SPSite mySite = new SPSite("http://servername:port");
                SPWeb myWeb = mySite.OpenWeb();
                SPContentTypeCollection myContentTypeCollection = myWeb.ContentTypes;
                SPContentType myContentType= new SPContentType(myContentTypeCollection[SPBuiltInContentTypeId.Item], myContentTypeCollection, "Content Type Name.....");
                myContentTypeCollection.Add(myContentType);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Adding the existing Site Columns to existing Content Type:

static void Main(string[] args)
        {
            try
            {
                SPSite mySite = new SPSite("http://servername:port");
                SPWeb myWeb = mySite.OpenWeb();
                SPFieldCollection myFieldCollection = myWeb.Fields;
                SPContentType myContentType=myWeb.ContentTypes["Content Type Name"];
                myContentType.FieldLinks.Add(new SPFieldLink(myFieldCollection["Employee Name"]));
                myContentType.FieldLinks.Add(new SPFieldLink(myFieldCollection["Designation"]));
                myContentType.Update();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Save site as template:

static void Main(string[] args)
        {
            try
            {
                SPSite mySite = new SPSite("http://servername:port");
                SPWeb myWeb = mySite.OpenWeb();
                myWeb.SaveAsTemplate("FiveNumber.stp", "FiveNumber", "Its All About SharePoint", true);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Delete Recycle Bin Items:

static void Main(string[] args)
        {
            SPSite mySite = new SPSite("http://servername:port");
            SPWeb myWeb = mySite.OpenWeb();
            SPRecycleBinItemCollection myRecycleBinCollection = mySite.RecycleBin;
            for (int i = 0; i < myRecycleBinCollection.Count; i++)
            {
                myRecycleBinCollection[i].Delete();
                i--;
            }
            Console.WriteLine("Press any key to continue.....");
            Console.ReadLine();
        }

Cannot start service SPUserCodeV4 on computer

28 August, 2010 (12:19) | SharePoint 2010 - Errors | By: G Vijai Kumar

If the service Microsoft SharePoint Foundation Sandboxed Code Service is stopped and when you are trying to deploy a webpart using Visual Studio 2010 we may face the error message saying that Cannot start service SPUserCodeV4 on computer <computer name>

Error - Cannot start service SPUserCodeV4 on Computer

Error - Cannot start service SPUserCodeV4 on Computer

To resolve this
Go to Central Administration > System Settings
On System Settings page, under Servers section click on Manage services on server

Start Service - Microsoft SharePoint Foundation Sandboxed Code Service

Start Service - Microsoft SharePoint Foundation Sandboxed Code Service

Next you will be able to notice the services in stopped/started status, look for the service Microsoft SharePoint Foundation Sandboxed Code Service and click Start, once you start then you will be able to deploy webpart or custom solutions created in Visual studio 2010

64 bit guest on 32 bit hosts

27 August, 2010 (09:37) | Sharepoint 2010 - General | By: G Vijai Kumar

64 bit processors handle large quantity of RAM extra efficiently compared to 32 bit processors. Microsoft announced that SharePoint 2010 supports only on 64 bit hardware. The grounds at the back is improving performance and elasticity of SharePoint

Public ask over whether they able to install SharePoint 2010 on 32 bit host, there is lot of puzzlement between people that SharePoint 2010 supports on 64 bit OS, so the answer can be Yes or No it depends on box.

If the 32 bit hosts supports virtualization and is enabled in BIOS then answer is Yes
If the 32 bit host does not support virtualization then the answer is No
All 32 bit box does not support virtualization.

If the CPU is few years older subsequently there is incredibly less probability of supporting 64 bit OS by means of virtual machine, on the other hand the majority of the modern processor support virtualization and can run 64 guest on 32 bit hardware via virtualization.

Therefore professionals, it is your call to decide whether your CPU supports virtualization or not, so first confirm before buying a new 64 bit PC

Please follow the link to Operating System Requirements of SharePoint 2010

FileNotFoundException was unhandled in Console Application

26 August, 2010 (15:25) | SharePoint 2010 - Errors | By: G Vijai Kumar

When I feel like to act together with SharePoint site to obtain data, quickly I will create a console application project to interact with SharePoint site instead of creating standard/visual webpart or custom solution

I am fond of console applications since there is no need of packaging the custom solution, deploying, activating etc. Just write the code and hit F5, if it run…runs, if not throws the error right away :-)

As I am comfortable with console applications in the same passion today I have created one console application project on SharePoint 2010 server and tried to list subsites from a site, the code be incredibly straightforward with not many lines, as soon as I hit F5 it thrown an error message saying that

FileNotFoundException was unhandled
The Web application at http://fivenumber:5 could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

FileNotFoundException was unhandled

FileNotFoundException was unhandled

The same code executes on MOSS 2007 server with out any problem.

I had Bing for a moment finally dig up the resolution, as there is hardware difference, between MOSS 2007 and SharePoint 2010.
Console applications by default targeted to x86 platform, therefore comprise to change the console application project properties to x64 or Any CPU

To resolve, right click on project and select Properties

Right click on project and select properties

Right click on project and select properties

Click on Build tab, under Platform target section select the option x64 or Any CPU then save the changes and run the code once again

Click on Build Tab

Click on Build Tab

Keep coding, Good Luck :-)