0

How can I add a link to a list item column that is of type hyperlink. The link should be a link to a subfolder in a document library.

What the program does it creates a new subfolder in a document library and after that I need to update a sharepoint list and create a list item with the details of the newly created document library subfolder, including a hyperlink to the subfolder. The list item field/column where the hyperlink is stored is labeled as File

I am using Microsoft.SharepointOnline.CSOM package with .net framework


Web web = clientContext.Web;
clientContext.Load(web, a => a.ServerRelativeUrl);
clientContext.ExecuteQuery();

// Create the subfolder in Sharepoint Document Library
Folder parentFolder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/" + parentFolderPath);
clientContext.Load(parentFolder);
clientContext.ExecuteQuery();

parentFolder.AddSubFolder(newSubFolderName, new ListItemUpdateParameters());
parentFolder.Folders.Add(newSubFolderName);
clientContext.Load(parentFolder);
clientContext.ExecuteQuery();

...

// Create a list item, need to add the hyperlink here in the field I have in my list called "File"
clientContext.Load(web.Lists,
        lists => lists.Include(list => list.Title, 
                               list => list.Id));

clientContext.ExecuteQuery();

List targetList = clientContext.Web.Lists.GetByTitle(listName);
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = targetList.AddItem(itemCreateInfo);

oListItem["File"] = ???

oListItem.Update();
clientContext.ExecuteQuery();

1 Answer 1

1

Try this

oListItem["File"] = new FieldUrlValue
{
  Url = "<your folder link here>"
  Description = "My New Folder"
}

Do you face any issues?

-- update: to get the URL to the folder try this:

var newFolder = parentFolder.AddSubFolder(newSubFolderName, new ListItemUpdateParameters());
clientContext.Load(newFolder);
clientContext.ExecuteQuery();

.....

oListItem["File"] = new FieldUrlValue
{
  Url = newFolder.ServerRelativeUrl
  Description = "My New Folder"
}

Sign up to request clarification or add additional context in comments.

4 Comments

The problem I have is that I dont know how to get the "<your folder link here>" value. I need the link to the new subfolder after I create it
Ah, I see. I have updated the answer
parentFolder.AddSubFolder returns void. I tried parentFolder.Folders.Add(subFulderName) and that returns it. However when I try to assign it to Url like oListItem["File"] = new FieldUrlValue, I get an Invalid Request error.
I had to convert the url to proper Uri format. It works now. Ty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.