Tuesday, July 17, 2012

Programatically modify a SharePoint List Item

The following code sample code provides a means for being able to perform actions on a list item in one of your SharePoint List or Document Library.  In this particular instance, I'm reviewing a specific item in a Document Library and performing an action that is dependent on the Check Out status of the associated file:

using Microsoft.SharePoint;

...

Guid siteId = new Guid(...);
Guid webId = new Guid(...);
Guid listId = new Guid(...);
Guid listItemId = new Guid(...);

using (SPSite site = new SPSite(siteId))
{
    using (SPWeb web = site.OpenWeb(webId))
    {
        SPList list = web.Lists[listId];
        SPListItem listItem = list.Items[listItemId];

        web.AllowUnsafeUpdates = true;
        if (listItem.File.CheckOutType.Equals(SPFile.SPCheckOutType.None))
        {
            // Perform the action associated with the file being checked in...
        }
        else
        {
            // Perform the action associated with the file being checked out...
        }
        listItem.Update();
        web.AllowUnsafeUpdates = false;
    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.