Sharepoint online CSOM generate list item display url

g h 716 Reputation points
2020-09-23T06:34:39.26+00:00

I have a file list inside desktop app thats fetched from the sharepoint online document library.
What i want to do is to provide a posibility to show that file in the browser. But i'm not able to generate proper url.
Can anyone provide an example code please ?
Thanks in advance.

Microsoft 365 and Office | SharePoint Server | Development
{count} votes

Accepted answer
  1. ZhengyuGuo 10,586 Reputation points Moderator
    2020-09-24T02:42:30.273+00:00

    Hi @g h ,

    Here is the sample code to get files' absolute url in library for your reference:

       using (ClientContext ctx = new ClientContext("https://zheguo.sharepoint.com/sites/dev/"))  
        {  
    
            ctx.Credentials = new SharePointOnlineCredentials(account, secret);  
            ctx.Load(ctx.Web);  
            ctx.ExecuteQuery();  
    
            List targetList = ctx.Web.Lists.GetByTitle("Documents");  
    
            ListItemCollection ItemCol = targetList.GetItems(CamlQuery.CreateAllItemsQuery());  
            ctx.Load(ItemCol);  
            ctx.ExecuteQuery();  
                foreach (Microsoft.SharePoint.Client.ListItem item in ItemCol)  
                {  
                    if (item.FileSystemObjectType == FileSystemObjectType.File)  
                    {  
                        Console.WriteLine(new Uri(ctx.Url).GetLeftPart(UriPartial.Authority) + item["FileRef"]);  
                    }  
    
                }  
        }  
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. sadomovalex 3,636 Reputation points
    2020-09-23T14:29:06.407+00:00

    hi, you need to load list.RootFolder (and list.RootFolder.Folders if you want to get files from all folders), then iterate through folder.Files and for each file you will get file.ServerRelativeUrl. Example of the script which uses CSOM for downloading all files in doclib can be found here: Download all files from a document library using client object model. Before to download files it gets downloadable url of each file - this is what you need.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.