【问题标题】:SharePoint URL retrieval for SPListItemSPListItem 的 SharePoint URL 检索
【发布时间】:2013-12-29 04:58:44
【问题描述】:

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

当我拉item["ColumnName"] 时,我得到了它的值:

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

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

【问题讨论】:

    标签: sharepoint list


    【解决方案1】:

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

    SPFieldUrlValue fieldValue = new SPFieldUrlValue(myItem["URL"].ToString());
    
    string linkTitle = fieldValue.Description;
    
    string linkUrl = fieldValue.Url;
    

    【讨论】:

      【解决方案2】:

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

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

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

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

      或者

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

      或者

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

      【讨论】:

      • 这将返回用于直接在页面上呈现字段值的 HTML 代码
      【解决方案3】:

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

      要获取完整的 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/

      【讨论】:

        【解决方案4】:

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

        来自 SharePoint 文档:

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

        您必须将字符串拆分为两部分。

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-26
          • 1970-01-01
          • 1970-01-01
          • 2012-05-04
          • 1970-01-01
          • 1970-01-01
          • 2014-08-16
          • 2012-06-15
          相关资源
          最近更新 更多