I need to add a new document link to a document library using the client object model. I suspect I could copy the format of the .aspx file used in the "link to a document" items and upload it as a normal file but I'm hoping there is a neater way.

So what I want to do is the same as the code sample below but instead of creating a new document I want to create a new link to a document.

ClientContext context = new ClientContext("http://wdev2008:22880");
List list = context.Web.Lists.GetByTitle("Test Doc Link Library");

FileCreationInformation fileCreateInfo = new FileCreationInformation();
fileCreateInfo.Content = new byte[] { 0x33 };
fileCreateInfo.Url = "zerobyte";
File file = list.RootFolder.Files.Add(fileCreateInfo);

Update: According to the mod on this forum I'm not allowed to ask how to do this with a web service in a separate question so I'll change the title, tags and content on this question. Does anyone know how to do the insert with a SharePoint web service or server side control or anything else?

  • I've merged your later question with this one, as it was essentially the same question. Please don't post the same question more than once. Oct 13 '10 at 2:44
  • Except that it was a different question aimed at those who are familiar with the SP web services not the client object model which are pretty DIFFERENT. Methinks you've got a bit too much ego and spare time on your hands! Perhaps you could answer questions instead.
    – sipwiz
    Oct 13 '10 at 3:51

Turns out it can be done but it's a bit of a hack that involves taking a copy of the .aspx file SharePoint uses for document links and doing a string search and replace on it Creating a 'Link to a Document' Item in a SharePoint Document Library programmatically.

I tailored the example to use the client object model:

 string docLinkTemplate = null;
 using (System.IO.StreamReader sr = new System.IO.StreamReader("SharePointDocLinkTemplate.txt"))
     docLinkTemplate = sr.ReadToEnd();

 string docLink = "http://myserver/test.doc";
 docLinkTemplate = String.Format(docLinkTemplate, docLink);

 FileCreationInformation fileCreateInfo = new FileCreationInformation();
 fileCreateInfo.Content = Encoding.UTF8.GetBytes(docLinkTemplate);
 fileCreateInfo.Url = "testlinkfordoc.doc.aspx";
 File file = list.RootFolder.Files.Add(fileCreateInfo);
 ListItem fileListItem = file.ListItemAllFields;
 fileListItem["ContentType"] = "Link to a Document";
 FieldUrlValue urlValue = new FieldUrlValue();
 urlValue.Description = "Link to doc test";
 urlValue.Url = docLink;
 fileListItem["URL"] = urlValue;

(Hopefully the mod won't deem this answer unworthy again. Pity the mods on these programming web sites don't have to get stackoverflow rep before they can start throwing their weight around.)

Your Answer

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