-
-
Save ZmanYo/e78532e748d0cb9ca038b685f3e96359 to your computer and use it in GitHub Desktop.
C# Sample to capture version of Document Sets using CSOM in SharePoint Online. It also lists out all the Document Set content type items along with the version information from a document library using the latest CSOM library released recently (version: 16.1.20317.12000 or above).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.SharePoint.Client; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Security; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace CSOM.ConsoleApp.Sample | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string userName = "MeganB@contoso.OnMicrosoft.com"; | |
SecureString password = ConvertToSecureString("password"); | |
var ctx = new ClientContext("https://contoso.sharepoint.com/sites/ModernTeamSite"); | |
ctx.Credentials = new SharePointOnlineCredentials(userName, password); | |
ctx.Load(ctx.Web); | |
ctx.Load(ctx.Web.Webs); | |
ctx.ExecuteQuery(); | |
Console.WriteLine(ctx.Web.Title); | |
var list = ctx.Web.Lists.GetByTitle("Documents"); | |
//var item = list.GetItemById(26); | |
//ctx.Load(item, l=>l.ContentType); | |
var query = new CamlQuery() | |
{ | |
ViewXml = String.Format("<View><Query><Where><Eq><FieldRef Name='ContentType' /><Value Type='Computed'>Document Set</Value></Eq></Where></Query></View>") | |
}; | |
ListItemCollection items = list.GetItems(query); | |
ctx.Load(items); | |
ctx.ExecuteQuery(); | |
foreach (var item in items) | |
{ | |
Console.WriteLine("Item id: "+ item.Id); | |
var folder = item.Folder; | |
ctx.Load(folder); | |
var ds = Microsoft.SharePoint.Client.DocumentSet.DocumentSet.GetDocumentSet(ctx, folder); | |
ctx.Load(ds); | |
var dsVersions = ds.VersionCollection; | |
ctx.Load(dsVersions); | |
ctx.ExecuteQuery(); | |
Console.WriteLine("Version count: "+ dsVersions.Count); | |
//Add new version. First parameter signifies whether to capture items within docset with their major or minor versions checked in. Second parameter is a string/comment. | |
dsVersions.Add(true, "capture new version thru CSOM"); | |
ctx.ExecuteQuery(); | |
//To get contents of a captured version of a Document set along with the site columns (fields) for the library. | |
List<Microsoft.SharePoint.Client.DocumentSet.DocumentSetVersionItem> itemsWithinDS = (List<Microsoft.SharePoint.Client.DocumentSet.DocumentSetVersionItem>)dsVersions[0].GetDisplayContents(); | |
List<Microsoft.SharePoint.Client.DocumentSet.DocumentSetVersionField> fields = (List<Microsoft.SharePoint.Client.DocumentSet.DocumentSetVersionField>)dsVersions[0].GetDisplayFields(); | |
ctx.ExecuteQuery(); | |
Console.WriteLine("Contents count: " + itemsWithinDS.Count); | |
Console.WriteLine($"Field title: {fields[0].Title}, Value: {fields[0].FormattedValue}"); | |
//break; | |
} | |
ctx.Dispose(); | |
Console.ReadLine(); | |
} | |
private static SecureString ConvertToSecureString(string password) | |
{ | |
if (password == null) | |
throw new ArgumentNullException("password"); | |
var securePassword = new SecureString(); | |
foreach (char c in password) | |
securePassword.AppendChar(c); | |
securePassword.MakeReadOnly(); | |
return securePassword; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment