Tuesday, February 2, 2016

SP2010 C# CSOM: Update Created and Modified dates and author

Tested on SharePoint 2010 only. I wanted to have something that could run on a client machine that could help update metadata for a folder or file in a list / document library.

This is a console application written in C#. Obviously, to do something in SharePoint 2010 automation, you will need to download the SP2010 foundation and install some runtime components.

(Or if you are feeling lazy, just copy Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll and reference them in your project).

To make this piece of code work, you need to know the ID of your list/folder item. There are other methods of retrieving based on URL.



Before you start, import the following:
 using Microsoft.SharePoint;  
 using Microsoft.SharePoint.Client;  
 using SP = Microsoft.SharePoint.Client;  
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.IO;  
 using System.Text.RegularExpressions;  

Here is the function which in turn calls another function that performs the actual updating.

 private static void UpdateMetadataSingle(string itemId, string author, string tsC, string tsM, string siteUrl, string listTitle)  
     {  
       int id;  
       int.TryParse(itemId, out id);  
       if (id > 0)  
       {  
         Console.WriteLine("Attemping to update {0}", itemId);  
         ClientContext clicon = new ClientContext(siteUrl);  
         ClientContext rootContext = new ClientContext(siteUrl);  
         SP.List list = clicon.Web.Lists.GetByTitle(listTitle);  
         clicon.Load(list);  
         list.EnableVersioning = false;  
         list.Update();  
         clicon.ExecuteQuery();  
         SP.ListItem item = list.GetItemById(id); //here item ID updating a single item  
         UpdateMetadata(clicon, item, author, tsC, tsM);  
       }  
     }  

Function to update metadata of list item/document/folder

     private static void UpdateMetadata(ClientContext clicon, ListItem item, string authorId, string created, string modified)  
     {  
       clicon.Load(item);  
       FieldUserValue theAuthor;  
       try  
       {  
         theAuthor = GetUsers(clicon, "SHAREPOINT\\" + authorId);  
       }  
       catch (Exception)  
       {  
         theAuthor = GetUsers(clicon, "SHAREPOINT\\system");  
       }  
       item["Created"] = Convert.ToDateTime(created).ToString("g");  
       item["Editor"] = theAuthor;  
       item["Modified"] = Convert.ToDateTime(modified).ToString("g");  
       item["Author"] = theAuthor;  
       item.Update();  
       clicon.ExecuteQuery();  
     }  

Function to pass in the login name and retrieve the SP user

    private static SP.FieldUserValue GetUsers(ClientContext clientContext, string UserName)  
     {  
       SP.FieldUserValue userValue = new SP.FieldUserValue();  
       SP.User updateUser;  
       try  
       {  
         updateUser = clientContext.Web.EnsureUser(UserName);  
       }  
       catch (Exception)  
       {  
         updateUser = clientContext.Web.EnsureUser("SHAREPOINT\\system");  
       }  
       clientContext.Load(updateUser);  
       clientContext.ExecuteQuery();  
       userValue.LookupId = updateUser.Id;  
       return userValue;  
     }  

Here is a simple console example of how you would call the function

 static void Main(string[] args)  
     {  
       if (args.Length > 0)  
       {  
         Console.WriteLine("Parsing input");  
         string id = args[0]; // the id of list item  
         string author = args[1]; // User Login Name without domain prefix  
         string tsC = args[2]; // created e.g. "2010-09-02 20:12:22.000"  
         string tsM = args[3]; // modified e.g. "2010-09-02 20:12:22.000"  
         string url = args[4]; // http://legalteam  
         string listTitle = args[5]; //BOD  
         UpdateMetadataSingle(id, author, tsC, tsM, url, listTitle);  
       }  
     }  

You can invoke the application and pass in the parameters: e.g.

 YourConsole.exe "1" "<UserLogonName>" "2010-01-18 01:16:11.000" "2010-01-18 01:16:11.000" "htt  
 p://sharepoint" "Shared Documents"  

No comments:

Related Posts Plugin for WordPress, Blogger...