none
Preserve Document Metadata while Sending File to Record Center using WebService

    Question

  • What I would like to achieve: In Record management space in Office 365, we would like to send the document from  different SharePoint Online site collection to Record Center. “Send To” connection option is only available in “Classic Experience” in UI.In Modern Experience, I would like to achieve the same “Send to” functionality pro grammatically.

    Approach:I have created a CSOM console application and used Web service and used SubmitFile() method to send the documents. Web Service Address: https://siteURL /sites/recordscenter/_vti_bin/officialfile.asmx

    SubmitFile():

    Reference:  https://msdn.microsoft.com/en-us/library/aa981121(v=office.12).aspx

    Issue: When I use SubmitFile() method which is used by  Microsoft’s record management submission point as below, 

    https://siteURL /sites/recordscenter/_vti_bin/officialfile.asmx

    I am able to move documents from SharePoint Online Site  to record center but the document metadata is not preserved

    Request: Can you please advise how I can preserve metadata of the document at target location.

    Code Sample:

    using Microsoft.SharePoint.Client;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Security;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Office365.ConsoleApps
    {
        class Program
        {  
            static void Main(string[] args)
            {
                Uri sourceSite = new Uri("sourceSiteURL");
                Uri targetSite = new Uri(@"https://siteName/sites/recordscenter");
                string srcDocLibUrl = @"https://SiteName/sites/Apps/Shared%20Documents/Doc2.docx";             
                string sourceLibrary = "Documents";
                int listItemId = 4;
                string userName = "userName";
                string recordRouting = "Document";       
                string sendToConnectionURL = "https://siteName/sites/recordscenter/_vti_bin/officialfile.asmx";
                Program p = new Program();
    
                p.SendDocumentToRecordCenter(sourceSite,targetSite, srcDocLibUrl, sourceLibrary, listItemId, userName, recordRouting, sendToConnectionURL);  
           }
    
    
            public void SendDocumentToRecordCenter(Uri sourceSite, Uri targetSite,string srcDocLibUrl,string sourceLibrary, int listItemId,string userName,string recordRouting,string sendToConnectionURL)
            {              
              using (var clientContext = new ClientContext(sourceSite))
                {
    
                    clientContext.Credentials = GetSharePointOnlineCredentials();
                    var list = clientContext.Web.Lists.GetByTitle(sourceLibrary);
                    var listItem = list.GetItemById(listItemId);
                    clientContext.Load(list);
                    clientContext.Load(listItem, i => i.File, i => i.ContentType);
                    clientContext.ExecuteQuery();           
                     var fileRef = listItem.File.ServerRelativeUrl;
                    var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
                    var stream = fileInfo.Stream;
    
                    IList<byte> content = new List<byte>();
                    int b;
    
                    while ((b = fileInfo.Stream.ReadByte()) != -1)
                    {
                        content.Add((byte)b);
                    }
    
                    byte[] fileContent = content.ToArray();
                    RecordsManager.RecordsRepository repository = new RecordsManager.RecordsRepository();
                    repository.Url = sendToConnectionURL;
    
                    repository.CookieContainer = GetAuthCookies(targetSite);
    
                    List<RecordsManager.RecordsRepositoryProperty> properties = new List<RecordsManager.RecordsRepositoryProperty>();
                    properties.Add(new RecordsManager.RecordsRepositoryProperty()
                    { Name = "Title", Type = "Text", Value = "Doc1" });
    
                    properties.Add(new RecordsManager.RecordsRepositoryProperty()
                    { Name = "Name", Type = "Text", Value = "Doc1"});
    
                    properties.Add(new RecordsManager.RecordsRepositoryProperty()
                    { Name = "ContentType", Type = "Text", Value = "Document" });
    
                    properties.Add(new RecordsManager.RecordsRepositoryProperty()
                    { Name = "Created", Type = "DateTime", Value = " 1 / 16 / 2016 6:18:53 PM" });
    
                    properties.Add(new RecordsManager.RecordsRepositoryProperty()
                    { Name = "vti_modifiedby", Type = "String", Value = "user_Name" });
              
                    try
                    {
                        string result = repository.SubmitFile(fileContent, properties.ToArray(), recordRouting, srcDocLibUrl, userName);
                        Console.WriteLine(result);
                        Console.ReadLine();
                    }
    
                    catch (Exception e)
                    {
                        throw e;
    
                    }
    
                }
    
            }
    
            public static SharePointOnlineCredentials GetSharePointOnlineCredentials()   
         {
                var login = "LoginName";
                var password = "Password";
                var securePassword = new SecureString();
                foreach (char c in password)
                { securePassword.AppendChar(c); }
                return  new SharePointOnlineCredentials(login, securePassword);
    
            }
    
            private static CookieContainer GetAuthCookies(Uri webUri)
            {
                var userName= "LoginName";
                var password = "Password";
                var securePassword = new SecureString();
                foreach (var c in password) { securePassword.AppendChar(c); }
    
                var credentials = new SharePointOnlineCredentials(userName, securePassword);
    
                var authCookie = credentials.GetAuthenticationCookie(webUri);
                var cookieContainer = new CookieContainer();
                cookieContainer.SetCookies(webUri, authCookie);
                return cookieContainer;
    
            }
    
        }
    
    }




    Monday, August 29, 2016 10:10 AM

All replies

  • Hi,

    The Created field seems not preserve, please try to add a new field("Test") in the source document library and add new field("Test") in the Drop Off Library in the Record Center, then add the code below in your code, then test it if it works.

    properties.Add(new RecordsManager.RecordsRepositoryProperty()
     { Name = "Test", Type = "Text", Value = "Test" });

    Best Regards,

    Dennis


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


    Wednesday, August 31, 2016 9:47 AM
    Moderator