Programmatically set the description field of a URL in a Links List
-
04-10-2020 - |
Вопрос
I have created a console application which creates a site collection and updates a links list in an existing sharepoint site which acts as a site register.
The code for adding a new entry to the list works and adds the url of the new site to my site register as intended. Here is the code I use to do this:
Console.WriteLine("Updating Workplace Site Register");
string siteUrl = "http://contoso.com";
ClientContext clientContext = new ClientContext(siteUrl);
SP.List oList = clientContext.Web.Lists.GetByTitle("Site Register");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);
oListItem["URL"] = mainurl;
oListItem.Update();
clientContext.ExecuteQuery();
Currently this causes the raw url of the new site to appear in the site register as a link. There is a Description
field attached to each URL in a links list which sets the display value of the new link.
How can I alter my code so that it also updates this field and my new link displays in a more user friendly fashion?
Решение
You can set it like this:
oListItem["URL"] = String.Format("{0}, {1}", mainurl, "Description");
It's a simple string and URL value is followed by a description which are separated by a comma.
Or you can use a more object oriented approach:
var value = new SPFieldUrlValue(mainurl);
value.Description = "Description";
oListItem["URL"] = value;
Whichever suits you :)
Другие советы
Try this:
FieldUrlValue url = new FieldUrlValue();
url.Url = "Your Url here";
url.Description = "Your description here";
oListItem["URL"] = url;