July 12, 2008

How to approve a folder programmatically in SharePoint

If you want to approve a file programmatically in SharePoint, you have to use the Approve(string comment) method of the SPFile class:

file.Approve(string.Format("This file has been approved the {0}", DateTime.Now));

If you want to approve a folder, it seems obvious that an Approve method also exists in the SPFolder class. Unfortunately, it is not the case. you have to use the ModerationInformation property of the underlying SPListItem:

using (SPSite site = new SPSite("http://server:port/"))
{
    SPWeb web = site.OpenWeb();
 
    using (web)
    {
        SPList myList = web.Lists["MyList"];
        SPFolder newFolder = myList.RootFolder.SubFolders.Add("folderName");
        SPModerationInformation moderationInformation = newFolder.Item.ModerationInformation;
        moderationInformation.Comment = string.Format("This folder has been approved the {0}",DateTime.Now);
        moderationInformation.Status = SPModerationStatusType.Approved;
        newFolder.Item.Update();
    }
}

2 comments:

ciaranc said...

Thank you for this.

Anonymous said...

Thanks thats work fine!