Skip to main content

Create and Update a hyperlink field in SharePoint using CSOM

To create and update a hyperlink field in SharePoint using CSOM and server side object model, following code can be used.

Create a hyperlink field using server object model

using (SPSite site = new SPSite("Site URL"))             {                 using (SPWeb web = site.OpenWeb())                 {                  SPList list = web.Lists.TryGetList("List Name");                    if (list != null)                     {                         SPListItem item = list.Items.Add();                        SPFieldUrlValue hyperlinkField = new SPFieldUrlValue();                        hyperlinkField.Description = "Title of Field";                         hyperlinkField.Url = "URL here";                         item["Title"] = "New item Name";                         item["Hyperlink"] = hyperlinkField;                         item.Update();                      }                 }             }

CSOM create a hyperlink field.


ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            var list = ctx.Web.Lists.GetByTitle("List Name");
            var listItem = list.AddItem(itemCreateInfo);
            listItem["Title"] = "New Item Title";
 Microsoft.SharePoint.Client.FieldUrlValue urlField = 
                     new Microsoft.SharePoint.Client.FieldUrlValue();
            urlField.Url = "URL here";
            urlField.Description = "Description text";            
            listItem["Hyperlink"] = urlField;            
            listItem.Update();            
            ctx.Load(listItem);            
            ctx.ExecuteQuery();

To update an existing hyperlink field

For server side object model

SPFieldUrlValue url = new SPFieldUrlValue();
url.Url = "URL path";
url.Description = "Description";
Listitem["HyperlinkFieldInternalName"] = url;

CSOM Code

FieldUrlValue url = new FieldUrlValue();
url.Url = "URL path";
url.Description = "Description";
Listitem["HyperlinkFieldInternalName"] = url;
ctx.Load(Listitem);            
ctx.ExecuteQuery();

Comments