0
votes

I am using Sharepoint 2010.

A document library has been created the Sharepoint portal.

I understand it is recommended that the Library url should not contain any spaces. So initially it is created as PPCompanies. And then for the library name to be more user friendly, this is renamed to SC Documents via Library Settings. The url does not change, it is still PPCompanies.

I find in my Client Object Model I have to access the library using the new Title, "PP Documents" with a space. However I want the url so that I can place a hyperlink to the document I have saved to the library. How do I do this? I would expect this to be a property of the Sharepoint.Client.List object, but there does not appear to be such a property that is set when I create it.

This is the code that saves the document to "PP Companies". The value "PP Companies" is found in this.DocumentLibrary;

    public void Save(byte[] file, string url)
    {
        using (var context = newClientContext(this.SharepointServer))
        {
            context.Credentials = newNetworkCredential(this.UserName, this.Password, this.Domain);
            var list = context.Web.Lists.GetByTitle(this.DocumentLibrary);
            var fileCreationInformation = newFileCreationInformation
                                              {
                                                  Content = file,
                                                  Overwrite = true,
                                                  Url = url
                                              };
            var uploadFile = list.RootFolder.Files.Add(fileCreationInformation);
            uploadFile.ListItemAllFields.Update();
            context.ExecuteQuery();
            if (this.Metadata.Count > 0)
            {
                this.SaveMetadata(uploadFile, context);
            }
        }
    }

    private void SaveMetadata(File uploadFile, ClientContext context)
    {
        var item = uploadFile.ListItemAllFields;
        foreach (var row inthis.Metadata)
        {
            item[row.Key] = row.Value;
        }

        item.Update();
        context.ExecuteQuery();
    }

So this code works. But I cannot use "PP Companies" to get the URL for the hyperlink I need after this has executed.

I have found that the context.Url is just the root folder url.

The list.RootFolder.ServerRelativeUrl is not set.

1

1 Answers

1
votes

When you use client context while working with SharePoint you need to use context.Load method before reading objects and their properties.

In your case add context.Load(list.RootFolder); just before ExecuteQuery method call. Then list object and RootFolder property will be sent from the server and accessible on client.