list SPListItem 的 SharePoint URL 检索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/612212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:20:12  来源:igfitidea点击:

SharePoint URL retrieval for SPListItem

sharepointlist

提问by Magnus Johansson

When I try to retrieve a column which is a hyperlink I get two items that are comma delimited instead of one.

当我尝试检索作为超链接的列时,我得到两个以逗号分隔的项目,而不是一个。

When I pull item["ColumnName"]I get its value:

当我拉item["ColumnName"]我得到它的价值:

http://www.google.com/article/583,%20title%20gets%20stars

http://www.google.com/article/583,%20title%20gets%20stars

Why is it showing the link, and title?

为什么它显示链接和标题?

回答by Magnus Johansson

You can extract the actual Urland the Descriptionfrom the column value this way:

您可以通过这种方式从列值中提取实际值UrlDescription值:

SPFieldUrlValue fieldValue = new SPFieldUrlValue(myItem["URL"].ToString());

string linkTitle = fieldValue.Description;

string linkUrl = fieldValue.Url;

回答by Rex M

Because at the lowest level, all Sharepoint fields are stored as strings. The GetFieldValue method of an SPField accepts a string, and it is up to the logic of that field class to read that string and convert it into a meaningful value object.

因为在最低级别,所有 Sharepoint 字段都存储为字符串。SPField 的 GetFieldValue 方法接受一个字符串,读取该字符串并将其转换为有意义的值对象取决于该字段类的逻辑。

item["FieldName"] returns a generic object that represents the field value. By itself the object is usually useless, except as the raw string representation of the data.

item["FieldName"] 返回一个表示字段值的通用对象。除了作为数据的原始字符串表示外,对象本身通常是无用的。

If you use the GetFieldValueAsHtml() method, it will return <a href="url">title</a>:

如果您使用 GetFieldValueAsHtml() 方法,它将返回<a href="url">title</a>

//if field is of type Hyperlink, returns <a href="url">title</a>
item.Fields["FieldName"].GetFieldValueAsHtml(item["FieldName"])

Or

或者

//if field is of type Hyperlink, returns Url, Title
item.Fields["FieldName"].GetFieldValueAsText(item["FieldName"])

Or

或者

//if field is of type Hyperlink, returns Url
item.Fields["FieldName"].GetValidatedString(item["FieldName"])

回答by K-M

The SPListItem URL property does return the URL for the SPListItem including the List/Documetn Library name, but it doesn't return the full URL including the server and site names.

SPListItem URL 属性确实返回 SPListItem 的 URL,包括 List/Documetn 库名称,但它不返回包括服务器和站点名称的完整 URL。

To get the full URL, you can either concatenate the SPLIstItem.Web.Url and the SPListItem.URL, or extract the full URL from the SPListItem.XML data like this:

要获取完整 URL,您可以连接 SPLIstItem.Web.Url 和 SPListItem.URL,或从 SPListItem.XML 数据中提取完整 URL,如下所示:

foreach (SPListItem item in list.Items)
{
  XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.LoadXml(item.Xml);

  XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable);
  nsm.AddNamespace("z", "#RowsetSchema");

  string fullURL = xmlDoc.SelectSingleNode("z:row", nsm).Attributes["ows_EncodedAbsUrl"].Value;
}

http://insomniacgeek.com/code/how-to-get-the-full-url-from-splistitem-in-sharepoint/

http://insomniacgeek.com/code/how-to-get-the-full-url-from-splistitem-in-sharepoint/

回答by markom

That is how SharePoint stores links. First the URL and then the Title that's actually shown on the page.

这就是 SharePoint 存储链接的方式。首先是 URL,然后是实际显示在页面上的标题。

From the SharePoint documentation:

从 SharePoint 文档:

"The URL field uniquely consists of two strings separated by a comma and space. One string contains the URL path and the other contains the description used as hyperlinked text."

“URL 字段由两个由逗号和空格分隔的字符串组成。一个字符串包含 URL 路径,另一个包含用作超链接文本的描述。”

You have to split the string to get two parts.

您必须拆分字符串以获得两部分。

string url = field["URL"].Split(',')[0];
string title = field["URL"].Split(',')[1];

Code is not optimal, but just to show you exactly what I mean.

代码不是最优的,只是为了向您展示我的意思。

Oliver, you didn't specify SharePoint version. My answer is for 2003 version. If you have MOSS, take a look at SPFieldUrland SPFieldUrlValueclasses.

奥利弗,你没有指定 SharePoint 版本。我的答案是 2003 版。如果您有 MOSS,请查看SPFieldUrlSPFieldUrlValue类。