动态在列表中插入查找字段

时间:2018-11-29 11:25:29

标签: c# sharepoint-2013

我有一个方案,用户将以预定义格式将Excel文件上载到在根Web中创建的文档库中。文档库中有SiteNameListName两个属性。现在,计时器作业将负责将数据插入属性中提供的“站点/列表”中的Excel文件中。

我面临的问题是目标列表中将有一些查找列,并且用户将仅提供Excel文件中的值而没有查找ID,并且由于站点和列表是动态的,因此我将无法查询查找列的源列表。

using (SPWeb web = site.RootWeb)
{
    SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists[BulkUploadLibraryName];

    SPQuery docLibQuery = new SPQuery();
    docLibQuery.Query = "<Where><Geq><FieldRef Name='Created'/><Value Type='DateTime'>" + DateTime.Now.AddDays(-1) + "</Value></Geq></Where>";
    SPListItemCollection collection = docLib.GetItems();

    foreach (SPListItem listItem in collection)
    {
        LinkFieldValue webField = (LinkFieldValue)listItem["SiteName"];
        LinkFieldValue listField = (LinkFieldValue)listItem["ListName"];
        var data = ExcelHelper.Read(web.Url + "//" + listItem.Url);
        List<ConfigItem> errorList = new List<ConfigItem>();

        using (SPWeb targetWeb = site.OpenWeb(webField.NavigateUrl))
        {
            SPList targetList = targetWeb.GetList(listField.NavigateUrl.Replace(" ", "%20"));                        

            foreach (ConfigItem configItem in data)
            {                            
                SPListItem item = targetList.Items.Add();
                SPFieldLookupValueCollection category = item["Category"] as SPFieldLookupValueCollection;
                SPFieldLookupValueCollection subCategory = item["SubCategory"] as SPFieldLookupValueCollection;

                item["Title"] = configItem.Title;
                item["City"] = configItem.City;
                item["Longitude"] = configItem.Longitude;
                item["Latitude"] = configItem.Latitude;
                item["Address"] = configItem.Address;
                item["Region"] = configItem.Region;
                item["WorkingDays"] = configItem.WorkingDays;
                item["WorkingHours"] = configItem.WorkingHours;
                item["LocationID"] = configItem.BranchID;
                item["Category"] = category;
                item["SubCategory"] = subCategory;
                try
                {
                    item.Update();
                }
                catch (Exception ex)
                {
                    configItem.Exception = ex.Message;
                    errorList.Add(configItem);
                }
            }
        }
    }
}

0 个答案:

没有答案