从SharePoint字段选择列中检索所有项目

时间:2009-08-17 20:59:19

标签: c# sharepoint

我正在使用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();
}

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

alt text

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

有人能指出我正确的方向吗?谢谢!

1 个答案:

答案 0 :(得分:8)

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;