Search code examples
c#sharepointsharepoint-2013

How to get FileID from Sharepoint


How can I get FileID from Sharepoint. The fileID that I'm looking for is the Guid that we required to pass in to the 'GetFileById' method. For example

var clientContext = new ClientContext("http://myserver");    
var fileGuid = new Guid("D51C440B-4F52-4005-90BE-BDC42E850975");
var file = clientContext.Web.GetFileById(fileGuid);

Additionally, I'm using Microsoft.SharePoint.Client from Nuget (version: 14.0.4762.1000) in a .NET console app to access sharepoint . I don't see the method 'GetFileById' anymore in this dll and due to this I'm using the below code to retrieve the sharepoint file object

using (ClientContext context = new ClientContext(new Uri("http://myserver")))
            {
                try
                {
                    context.AuthenticationMode = ClientAuthenticationMode.Default;
                    context.Credentials = CredentialCache.DefaultNetworkCredentials;
                  
                    Web web = context.Web;
                    Microsoft.SharePoint.Client.File doc = web.GetFileByServerRelativeUrl(docURL);

                    context.Load(doc, d => d.Name, d => d.Exists, d => d.ListItemAllFields);
                    context.ExecuteQuery();
                }
            }

How can I get fileID(Guid) from the above code snippet ? I tried using ListItemAllFields["_dlc_DocId"] but that is not fileID(Guid) that I'm looking for.

Any thoughts ?


Solution

  • Use ListItemAllFields["UniqueId"] to get the sharepoint FileID (Guid). See the below code

    using (ClientContext context = new ClientContext(new Uri("http://myserver")))
                {
                    try
                    {
                        context.AuthenticationMode = ClientAuthenticationMode.Default;
                        context.Credentials = CredentialCache.DefaultNetworkCredentials;
                      
                        Web web = context.Web;
                        Microsoft.SharePoint.Client.File doc = web.GetFileByServerRelativeUrl(docURL);
    
                        context.Load(doc, d => d.Name, d => d.Exists, d => d.ListItemAllFields);
                        context.ExecuteQuery();
                        Console.WriteLine(doc.ListItemAllFields["UniqueId"]);
                    }
                }