Tuesday, February 15, 2011

Programmatically add records in a List in SharePoint

using Microsoft.SharePoint;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("SITE URL"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["LIST NAME"];
web.AllowUnsafeUpdates = true;
SPListItem item = list.Items.Add();
item["TITLE"] = "Hello World";
item.Update();
list.Update();
web.AllowUnsafeUpdates = false;
}
}
});

Now for some explanation :

using Microsoft.SharePoint; This namespace is added at the start of the .cs file to use available classes. We have to first add a reference to Microsoft.SharePoint.dll file by right clicking your solutions/project/web application and selecting Add Reference option.
If you are working on SharePoint 2007 (MOSS) browse to 12 hive and then to ISAPI folder and select Microsoft.SharePoint.dll (C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\ISAPI). If you are working on SharePoint 2010, the folder is 14.

After adding reference importing the required namespace, we have a Security Context

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("SITE URL"))
{
using (SPWeb web = site.OpenWeb())
{
/
/do something there
}
}
});

If you are logged in as a user who does not have rights to perform any insert operation, we elevate privileges. This feature is provided in Windows SharePoint Services 3.0 to perform actions in code programmatically using an increased level of privilege.

using scope is introduced so that as soon as SPSite and SPWeb objects gets out of context, they are disposed of automatically

SPSite site = new SPSite("SITE URL")We first open the site in which we have the list on which we want to perform insert operation.

SPWeb web = site.OpenWeb()We open the web in which we have the list of which we want to perform insert operation.
SPList list = web.Lists["LIST NAME"];Now we create object of SPList Class with the list in which we have to perform insert operation. "LIST NAME" is the name of the list which needs to be accessed from your code.

web.AllowUnsafeUpdates = true; We get security validation errors if we do not set our web for AllowUnSafeUpdates.

SPListItem item = list.Items.Add();SPList class is collection of list items. Just like any other collection, say DataTable for instance, whenever we want to add a new item to collection, we call the Add method to return SPListItem object. Simply speaking, this object represents a row in the SPList list.

item["TITLE"] = "Hello World"; As we discussed above that SPListItem instance represents a row in your list, now you can access columns/fields and set valid values in them.

item.Update();
list.Update();
web.AllowUnsafeUpdates = false;After setting the values, we update our item, we finally update our list which inserts data in the list. We initially set web.AllowUnsafeUpdates to true to avoid errors. Now we are done with our insert operation, its better to set it to false again.
The code will automatically move to the next statement which is closing braces of our using block which disposes our SPWeb and SPSite objects.

No comments: