Showing posts with label Sharepiont. Show all posts
Showing posts with label Sharepiont. Show all posts

Thursday, December 22, 2011

SharePoint Batch Updating List Items Programatically

While working on a custom SharePoint solution, I had a requirement to update multiple list items one time rather than updating one by one.
Example: Update only the items in a list which doesn't have value for a custom column (update with some value).

When i think of the solution i couldn't make new SPListItemCollection object out of the items which needs to be updated, which is possible in regular c# application development.
However in SharePoint we can do the batch update of list items using CAML script.
The CAML script needs to be generated for the items which needs to be updated as below.

Get the list items:
SPListItemCollection items = spList.Items;
Generate CAML Script:
StringBuilder methodBuilder = new StringBuilder();

string batchFormat = "" +
"{0}"; //{0}-methodFormat

string methodFormat = "" + //{0}-Unique Value for each Item
"{1}" + //{1}-List Guid
"Save" +
"{2}" + //{2}-List ItemID
"{4}" + //{3}-Column Name, {4}-Column Value
"";

// Build the CAML update command script.
foreach (SPListItem item in items)
{
    //get the custom column value
    string customColumnValue = string.Empty;
    if (null != item["CustomColumnName"])
        customColumnValue = item["CustomColumnName"].ToString();
    
    //check whether custom column is empty
    if (string.IsNullOrEmpty(customColumnValue))
    {
        methodBuilder.AppendFormat(methodFormat, item.ID, item.ParentList.ID, item.ID, "CustomColumnName", "New Updated Value");
    }
}

//batch update script.  
string batchUpdateScript = string.Format(batchFormat, methodBuilder.ToString());
Execute CAML Script:
spWeb.ProcessBatchData(batchUpdateScript);

Similarly batch deleting script can also be generated. Look at the detailed post here for code.

Friday, August 26, 2011

How To Download Folder from SharePoint Document Library or List

Scenario: SharePoint document library download folder

Generally SharePoint is used for Content & Document Management System. If it is more of Document Management System, users will upload and download files and documents quite often to/from document library or list.

Downloading a single document or file is straight forward, by clicking on the file. But there is no straight way to download folder from SharePoint document library. And downloading one by one file from a library is hectic if it has has more number of files (say 100s).

However a folder with all the files can be downloaded easily by following the below steps:
1. Go to the document library or list.
2. Open Explorer View of the respective document library.
3. Drag & Drop the folder from library to local drive.
                               OR
    Select the folder -> right click -> copy, then paste it on the local drive.