0

I'm trying to create a link in a document library pointing to another document library.

Like this :

But I want to do it by code in C# for SharePoint Online. I've followed numerous guides but can't get it to work.

Web web = ctx2.Web;
var externalList = web.Lists.GetByTitle("LegalExternal");
ctx2.Load(externalList);
ctx2.ExecuteQuery();

string fileLink = @"https://*****.sharepoint.com/customer/Shared%20Documents/Forms/AllItems.aspx?";
                string aspxFile = GetLinkString();

                aspxFile = aspxFile.Replace("{0}", fileLink);
                FileCreationInformation fileInfo = new FileCreationInformation();
                fileInfo.Content = UTF8Encoding.UTF8.GetBytes(aspxFile);
                fileInfo.Url = "Test.aspx"; //also tried Test.aspx.url

                File file = externalList.RootFolder.Files.Add(fileInfo);

                ctx2.Load(externalList);
                ctx2.Load(file);
                ctx2.ExecuteQuery();

                ListItem item = file.ListItemAllFields;

                item["ContentType"] = "Link to a Document";
                FieldUrlValue itemUrl = new FieldUrlValue();
                itemUrl.Url = fileLink;
                item.FieldValues["URL"] = itemUrl;

                item.Update();

 public string GetLinkString()
        {
            string prop = "<%@ Assembly Name = 'Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' %>" +
                            "<%@ Register TagPrefix = 'SharePoint' Namespace='Microsoft.SharePoint.WebControls' Assembly='Microsoft.SharePoint' %>" +
                            "<%@ Import Namespace = 'System.IO' %>" +
                            "<%@ Import Namespace = 'Microsoft.SharePoint' %>" +
                            "<%@ Import Namespace = 'Microsoft.SharePoint.Utilities' %>" +
                            "<%@ Import Namespace = 'Microsoft.SharePoint.WebControls' %>" +
                            "<html xmlns:mso='urn:schemas-microsoft-com:office:office' xmlns:msdt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'>" +
                            "<Head><META Name='progid' Content='SharePoint.Link'>" +
                            "<xml>" +
                            "<mso:CustomDocumentProperties>" +
                            "<mso:URL msdt:dt='string'>{0}, {0}</mso:URL>" +
                            "<mso:ContentType msdt:dt='string'>Link to a Document</mso:ContentType>" +
                            "</mso:CustomDocumentProperties>" +
                            "</xml>" +
                            "</head>" +
                            "<body>" +
                            "<form id='Form1' runat='server'>" +
                            "<SharePoint:UrlRedirector id='Redirector1' runat='server'/>" +
                            "</form>" +
                            "</body>" +
                            "</html>";

           return prop;

}

I get no errors but can't get it to work

Your Answer

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

Browse other questions tagged or ask your own question.