Quantcast
Viewing all articles
Browse latest Browse all 10096

Preserve Document Metadata while Sending File to Record Center using WebService

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” option is only available in “Classic Experience” in UI.

In Modern Experience, I would like to achieve the same “Send to” functionality programmatically.

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 document to record center but the document metadata is not preserved

Request:

Can you please advise how I can preserve metadata

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;

        }

    }

}



Viewing all articles
Browse latest Browse all 10096

Trending Articles