如何使用Sharepoint 2010/2013客户端OM获取扩展属性

时间:2014-01-29 10:00:47

标签: sharepoint extended-properties

以下代码使用SP Server OM,我想知道如何使用客户端OM检索任务扩展属性。

list = web.Lists["Tasks"];
 SPQuery tasksQuery = new SPQuery();
            tasksQuery.Query = @"<Where>
                               <Or>
                                   <Eq>
                                       <FieldRef Name='Status' /><Value Type='Choice'>In Progress</Value>
                                   </Eq>
                                   <Eq>
                                       <FieldRef Name='Status' /><Value Type='Choice'>Not Started</Value>
                                   </Eq>
                               </Or>
                           </Where>";
SPListItemCollection tasksListItemCollection =list.GetItems(tasksQuery);
foreach (SPListItem item in tasksListItemCollection)
{
    Hashtable extendedProperties = SPWorkflowTask.GetExtendedPropertiesAsHashtable(item);
}

提前谢谢。

1 个答案:

答案 0 :(得分:2)

CSOM中没有SPWorkflowTask.GetExtendedPropertiesAsHashtable method的模拟,但它可以实现(反射器来拯救)。

如何通过SharePoint托管CSOM获取表示任务扩展属性集合的哈希表

    /// <summary>
    /// Gets a hash table that represents the task’s extended properties collection
    /// </summary>
    /// <param name="task"></param>
    /// <returns></returns>
    public static Hashtable GetExtendedPropertiesAsHashtable(ListItem task)
    {
        if (task == null)
        {
            throw new ArgumentNullException();
        }
        Hashtable properties = new Hashtable();
        string extProperties = (string)task["ExtendedProperties"];
        if (!string.IsNullOrEmpty(extProperties))
        {
            var reader = new XmlTextReader(new StringReader("<Root " + extProperties + " />"))
            {
                WhitespaceHandling = WhitespaceHandling.Significant
            };
            reader.MoveToContent();
            if (!reader.HasAttributes)
            {
                return properties;
            }
            while (reader.MoveToNextAttribute())
            {
                string propName = reader.Name.Substring(4);
                properties[propName] = reader.Value;
            }
        }
        return properties;
    }