【问题标题】:Retrieve all items from a SharePoint Field Choice Column从 SharePoint 字段选择列中检索所有项目
【发布时间】:2009-08-17 20:59:19
【问题描述】:

我正在使用 SharePoint 服务器,并尝试以编程方式将服务请求添加到 Microsoft 的呼叫中心应用程序模板。到目前为止,我取得了相当不错的成功。我可以为指定的客户添加呼叫并分配特定的支持技术:

private enum FieldNames
{
    [EnumExtension.Value("Service Request")]
    ServiceRequest,
    [EnumExtension.Value("Customer")]
    Customer,
    [EnumExtension.Value("Service Representative")]
    ServiceRepresentative,
    [EnumExtension.Value("Assigned To")]
    AssignedTo,
    [EnumExtension.Value("Software")]
    Software,
    [EnumExtension.Value("Category")]
    Category
}
private void CreateServiceCall(string serviceCallTitle, string customerName, string serviceRep)
{
    SPSite allSites = new SPSite(siteURL);
    SPWeb site = allSites.AllWebs[siteName];
    SPListItemCollection requestsList = site.Lists[serviceRequests].Items;
    SPListItem item = requestsList.Add();

    SPFieldLookup customerLookup = item.Fields[FieldNames.Customer.Value()] as SPFieldLookup;

    item[FieldNames.ServiceRequest.Value()] = serviceCallTitle;

    if (customerLookup != null)
    {
        using (SPWeb lookupWeb = allSites.OpenWeb(customerLookup.LookupWebId))
        {
            SPList lookupList = lookupWeb.Lists.GetList(new Guid(customerLookup.LookupList), false);
            foreach (SPListItem listItem in lookupList.Items)
            {
                if (listItem[customerLookup.LookupField].ToString() != customerName) continue;

                item[FieldNames.Customer.Value()] = new SPFieldLookupValue(listItem.ID, customerName);
                break;
            }
        }
    }
    SPUserCollection userCollection = site.SiteUsers;
    if (userCollection != null)
    {
        foreach (SPUser user in userCollection)
        {
            if (user.Name != serviceRep) continue;

            item[FieldNames.AssignedTo.Value()] = user;
            break;
        }
    }

    item.Update();

    site.Close();
    allSites.Close();
}

我在默认列表中添加了两个自定义列(类别、软件):

我在 SharePoint 中填充了这两列,现在我想检索该数据,以便可以在我发布的代码 sn-p 中使用它来为调用分配正确的类别/软件等。我无法在代码中获得列表,我尝试过使用item["Software"]site.Lists["Software"] 和其他几个,但到目前为止我想出的只是null

谁能指出我正确的方向?谢谢!

【问题讨论】:

    标签: c# sharepoint


    【解决方案1】:

    SPFieldMultiChoice 和相关字段有一个Choices 属性:

    SPFieldMultiChoice software = item.Fields[FieldNames.Software.Value()] as SPFieldMultiChoice;
    StringCollection softwareChoices = software.Choices;
    

    如果需要在字段上设置值,请使用SPFieldMultiChoiceValue 类型:

    SPFieldMultiChoiceValue values = new SPFieldMultiChoiceValue();
    values.Add("Choice 1");
    values.Add("Choice 2");
    item[FieldNames.Software.Value()] = values;
    

    【讨论】:

    • 这个例子演示了从SPListItem 中检索SPFieldMultiChoice 对象——注意你也可以从SPList 中获取这个对象,这对我来说更有用,因为我想访问在我获得 List 引用之后,但在我开始迭代 List 的项目之前,Choices 属性。只需按照示例中访问 item.Fields 的方式访问 SPList.Fields 属性即可。
    猜你喜欢
    • 2012-08-18
    • 2019-09-06
    • 2014-10-03
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多