1

How to perform 'View' operation for fetching all metadata values of the specific version using SharePoint client object model, for instance here version 7.1

enter image description here

I am working for Provider hosted app [SharePoint online], so any SPServices would not be beneficial.

Any Ideas ?

2

You can use below mentioned code to get the details version of specific item from SharePoint list using Client Object Model.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.Net;

namespace AccessVersionItems
{
    class Class1
    {
        File file;
        FileVersionCollection versions;
        ClientContext clientContext;
        IEnumerable<Microsoft.SharePoint.Client.FileVersion> oldVersions;
        public void GetVersion()
        {
            clientContext = new ClientContext("http://xxxxxxx/workspace");
            clientContext.Credentials = new NetworkCredential("aaaa", "bbbbbb");
            Web web = clientContext.Web;
            clientContext.Load(web);
            clientContext.ExecuteQuery();

                    string path = web.ServerRelativeUrl + "/Lists/Issue Report/7_.000";  // it represet list id 7
                    file = web.GetFileByServerRelativeUrl(path);

                    ListItem versionListItem = file.ListItemAllFields;file.ListItemAllFields.FieldValues["Defect_x0020_Name"]
                    clientContext.Load(versionListItem);
                    clientContext.ExecuteQuery();

                    versions = file.Versions;
                    clientContext.Load(versions);
                    oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
                    clientContext.ExecuteQuery();

                    if (oldVersions != null)
                    {
                        foreach (Microsoft.SharePoint.Client.FileVersion _version in oldVersions)
                        {

                            Console.WriteLine("Version : {0}", _version.VersionLabel);  // till working fine


                            File f = web.GetFileByServerRelativeUrl("/"+_version.Url.ToString());
                            clientContext.Load(f, ff => ff.ListItemAllFields);
                            clientContext.ExecuteQuery();
                            string a = "";
                        }
                    }
                    Console.ReadKey();
                }
    }
}

Note:-

Please change all server & list details (in variables) according to your server

UPDATED

Please replace below code

File f = web.GetFileByServerRelativeUrl("/"+_version.Url.ToString());

with

string versionItemUrl = file.ServerRelativeUrl.Replace(Path.GetFileName(file.ServerRelativeUrl),"") + _version.Url;
File oldFile = web.GetFileByServerRelativeUrl(versionItemUrl);

& try again.

  • Have you got it working? – Hardik Jun 8 '16 at 14:38
  • Thanks @Hardik for the code, I tried with this and getting error 'The object specified does not belong to a list.' on this line 'File f = web.GetFileByServerRelativeUrl("/sites/dev/"+_version.Url.ToString());' where my string is build up like '/sites/dev/_vti_history/512/Global Lib/ARCHITECTURAL STANDARDS.docx' – Kaushal15 Jun 9 '16 at 14:06
  • Please check updated code in my answer. – Hardik Jun 9 '16 at 14:12
  • the problem ended up being that an SPFile was constructed in a parent web instead of the web in which the actual file resided using SPWeb.GetFile(url). So to fix the problem, make sure you construct your SPFile object (using GetFile anyway) in the SPWeb in which the file resides. eg. if the file lives here: \ParentSite\SubSite\Pages\Page.aspx, make sure the SPWeb you are using is "Subsite". – Hardik Jun 9 '16 at 14:15
  • Did you tried updated code? is that works for you? – Hardik Jun 9 '16 at 14:39
0
#region Using Directives

using System;
using System.IO;
using System.Linq;
using SP = Microsoft.SharePoint.Client;

using System.Net;

#endregion

namespace SharePointClient
{
    class Program
    {
        static void Main(string[] args)
        {
            string srcUrl = "https://aaa/bbb/ccc/";
            string destUrl = "https://aaa/bbb/ddd/";;
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("aaa", "ccc", "vvvv");

            string srcLibrary = "MySrc Library";
            string destLibrary = "MyDest Library";

            //copy latest full version of file
            CopyDocuments(srcUrl, destUrl, srcLibrary, destLibrary, credentials);      
        }

        #region Supporting Methods


        public static void CopyDocuments(string srcUrl, string destUrl, string srcLibrary, string destLibrary, System.Net.NetworkCredential credentials)
        {
            // set up the src client
            using (SP.ClientContext srcContext = new SP.ClientContext(srcUrl))
            {
                // set up the destination 
                using (SP.ClientContext destContext = new SP.ClientContext(destUrl))
                {
                    srcContext.Credentials = credentials;
                    destContext.Credentials = credentials;

                    // get the source web
                    SP.Web srcWeb = srcContext.Web;                   
                    SP.List srcList = srcWeb.Lists.GetByTitle(srcLibrary);
                    // SP.CamlQuery query = SP.CamlQuery.CreateAllItemsQuery(5);
                    //
                    SP.CamlQuery query = new SP.CamlQuery();     
                    query.ViewXml = @"<View><Query><Where><Eq><FieldRef Name='ICD_x0020_ID' /><Value Type='Text'>6382</Value></Eq></Where></Query></View>";

                    SP.ListItemCollection srcItems = srcList.GetItems(query);
                    srcContext.Load(srcItems);
                    srcContext.ExecuteQuery();

                    // get the destination web
                    SP.Web destWeb = destContext.Web;
                    destContext.Load(destWeb);
                    destContext.ExecuteQuery();                    

                    foreach (var item in srcItems)
                    {
                        try
                        {
                            if (item.FileSystemObjectType == SP.FileSystemObjectType.File)
                            {
                                // get the first file version
                                SP.File file = item.File;
                                SP.FileVersionCollection versions = file.Versions;
                                srcContext.Load(versions);
                                srcContext.ExecuteQuery();
                                //SP.FileVersion firstVersion = versions.OrderBy(L => L.ID).FirstOrDefault();
                                SP.FileVersion firstVersion = versions.OrderBy(L => L.ID).Last();
                                //
                                foreach (SP.FileVersion _version in versions)
                                {
                                    Console.WriteLine("Version :{0}\t", _version.VersionLabel);
                                }

                                //
                                if (destContext.HasPendingRequest)
                                    destContext.ExecuteQuery();

                                string filePath = Path.Combine(@srcUrl, firstVersion.Url).Replace(" ", "%20");
                                string fileName = Path.GetFileName(filePath);

                                // build new location url
                                string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "%20") + "/" + fileName;

                                WebClient client = new System.Net.WebClient();
                                client.Credentials = credentials;

                                using (Stream fileRef = client.OpenRead(filePath))
                                {
                                    SP.File.SaveBinaryDirect(destContext, nLocation, fileRef, true);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("File Error = " + ex.ToString());
                        }
                    }
                }
            }
        }




        #endregion
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.