none
Move Document Set - Inquiry RRS feed

  • Question

  • Hello Everyone,

    I am relatively new to the adventures of the SharePoint API. I recently came across some code on the internet in regards to moving a document set. The source of the code is from several blogs I have come across as I did not write the code however it appeared to accomplish what I was looking to test out. I am pasting the code below...

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.IO;  
    4. using System.Linq;  
    5. using System.Text;  
    6. using System.Threading.Tasks;  
    7. using Microsoft.SharePoint.Client;  
    8. using Microsoft.SharePoint.Client.DocumentSet;  
    9. namespace DocSetCSOM {  
    10.     class Program {  
    11.         static void Main(string[] args) {  
    12.             if (args.Length != 2) {  
    13.                 Console.WriteLine("Usage: DocSetCSOM.exe <newDocSetName> <docSetContentTypeName>");  
    14.                 return;  
    15.             }  
    16.             ClientContext context = new ClientContext("URL");  
    17.             ContentTypeCollection contentTypes = context.Web.ContentTypes;  
    18.             context.Load(contentTypes);  
    19.             context.ExecuteQuery();  
    20.             ContentType docSetContentType = null;  
    21.             foreach(ContentType ct in contentTypes) {  
    22.                 if (ct.Name == args[1]) {  
    23.                     docSetContentType = ct;  
    24.                     break;  
    25.                 }  
    26.             }  
    27.             List list = context.Web.Lists.GetByTitle("Documents");  
    28.             List target = context.Web.Lists.GetByTitle("target");  
    29.             Folder folder = context.Web.GetFolderByServerRelativeUrl("target/xyz");  
    30.             ListItem listItem = list.GetItemById(2);  
    31.             DocumentSet docSet = DocumentSet.GetDocumentSet(context, listItem.Folder);  
    32.             ClientArrayResult < byte > data = docSet.ExportDocumentSet();  
    33.             context.ExecuteQuery();  
    34.             DocumentSet newDocSet = DocumentSet.ImportDocumentSet(context, data.Value, args[0], folder, docSetContentType.Id, context.Web.CurrentUser);  
    35.             context.ExecuteQuery();  
    36.             Console.WriteLine("Done");  
    37.         }  
    38.     }  
    39. }  

    Basically what the code is doing is moving a doc set from one doc lib to another. The issue I am having is I cannot get this code to fire off sans exception. I went ahead and made sure I grabbed the latest assemblies via Install-Package Microsoft.SharePointOnline.CSOM PM CMD. I thought it was perhaps a GACing issue so I swapped asms. I did notice this snippet in the above code...

    'ClientArrayResult < byte > data = docSet.ExportDocumentSet();'

    That line is where the original issue stems as the exception is you cannot convert system.io.stream to 'ClientArrayResult < byte >. If you drill into the ImportDocumentSet method the parameter should be of the Stream data type, so the exception makes sense. If you flip to Stream the the exception will be throw on the following line...

    context.ExecuteQuery();

    The exception thrown there is stating that Method "GetDocumentSet" does not exist. If I comment that out then the 'archiveStream' parameter is passed as a null value. I am at a loss as to what can be the issue here. I am looking for any ideas or pointers etc. Any help would be greatly appreciated. Thanks!



    • Edited by theGBSN3219 Thursday, March 30, 2017 4:39 AM add contents
    Thursday, March 30, 2017 4:36 AM

All replies

  • Hi,

    The latest assemblies which downloaded from Package Manager are not supported to be used in SharePoint on-premises environment, it is only available in SharePoint Online currently:

    https://msdn.microsoft.com/en-us/library/office/microsoft.sharepoint.client.documentset.documentset.exportdocumentset.aspx?f=255&MSPPError=-2147217396

    A code snippet like this can move the document set across libraries in my SharePoint Online site successfully:

    ...........
    ContentTypeCollection contentTypes = clientContext.Web.ContentTypes;
    clientContext.Load(contentTypes);
    clientContext.ExecuteQuery();
    ContentType docSetContentType = null;
    foreach (ContentType ct in contentTypes)
    {
        Console.WriteLine(ct.Name);
        if (ct.Name == "Document Set")
        {
            docSetContentType = ct;
            break;
        }
    }
    
    List list = clientContext.Web.Lists.GetByTitle("LB03311");
    //List target = clientContext.Web.Lists.GetByTitle("LB03312");
    Folder folder = clientContext.Web.GetFolderByServerRelativeUrl("/sites/patrick/LB03312");
    
    ListItem listItem = list.GetItemById(2);
    DocumentSet docSet = DocumentSet.GetDocumentSet(clientContext, listItem.Folder);
    ClientResult<System.IO.Stream> data = docSet.ExportDocumentSet();
    clientContext.ExecuteQuery();
    
    DocumentSet newDocSet = DocumentSet.ImportDocumentSet(clientContext, data.Value, "Imported", folder, docSetContentType.Id);
    clientContext.ExecuteQuery();
    Console.WriteLine("Done");


    Best regards,

    Patrick 

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, March 31, 2017 9:27 AM
    Moderator