2017-02-09 67 views
2

我试图将数据从一个列表复制到另一个列表(这两个列表位于不同的站点)以及查找列。但是,我得到一个错误的查询字段为:值不在预期范围内 - SharePoint查找字段的例外

值不在预期范围内

代码的工作和数据被复制其他非查找字段。我尝试了所有可能的方式,包括增加列表视图查找阈值和所有可能的代码方式,但仍然错误仍然存​​在于ExecuteQuery()

下面是我对查找字段代码:

if (field is FieldLookup && field.InternalName == "Country") 
{ 
    var CountryLookup = (item.FieldValues["Country"] as FieldLookupValue).LookupValue.ToString(); 
    var CountryLookupId = (item.FieldValues["Country"] as FieldLookupValue).LookupId.ToString(); 
    FieldLookupValue flvRDS = new FieldLookupValue(); 
    flvRDS.LookupId = int.Parse(CountryLookupId); 
    itemToCreate["Country"] = flvRDS; 
    itemToCreate.Update(); 
    destContext.ExecuteQuery(); 
} 

帮助真的赞赏。

回答

0

我认为item是您尝试在目标列表上创建的新ListItem。

但是你实际上从来没有从field这里读取任何值!所以基本上,你试图用项目[“Country”]来设置你的新FieldLookup.LookupId。LookupId在这个时候在逻辑上应该是空的。

下面是我用来从一个值中检索查找字段ListItem的方法,随意修改它以适合您的需要,因为我不知道如何检索它(SPList是Microsoft.SharePoint的别名.Client.List)。

private ListItem GetLookupItem(FieldLookup lookupField, string lookupValue) 
{ 
    string mappingField = lookupField.LookupField; 

    Microsoft.SharePoint.Client.List lookupList = Context.Web.Lists.GetById(new Guid(lookupField.LookupList)); 

    Context.Load(lookupList); 
    Context.ExecuteQuery(); 

    ListItemCollection libListItems = lookupList.GetItems(CamlQuery.CreateAllItemsQuery()); 
    Context.Load(libListItems, items => items.Include(
     itemlookup => itemlookup.Id, 
     itemlookup => itemlookup[mappingField])); 
    Context.ExecuteQuery(); 

    foreach (ListItem mappedItem in libListItems) 
    { 
     object mappedField = mappedItem[mappingField]; 
     if (mappedField != null && mappedField.ToString().Equals(lookupValue)) 
      return mappedItem; 
    } 

    return null; 
} 

现在,你有相应的列表项,你可以用它的ID设置你的item.LookupId

if (field is FieldLookup && field.InternalName == "Country") 
{ 
    FieldLookupValue flvRDS = new FieldLookupValue(); 
    flvRDS.LookupId = GetLookupItem(field as FieldLookup, "France").Id; // here, dunno how you get your country's name 
    itemToCreate["Country"] = flvRDS; 
    itemToCreate.Update(); 
    destContext.ExecuteQuery(); 
} 

随意,如果你想为你的特定问题的答案更适合多增加一些以前的代码。

+0

实际上,item是我阅读的列表项中的现有数据,itemToCreate是我需要复制它的新项目。另外,我在CountryLookup&CountryLookupId中分别获取国家名称和ID。只是他们没有被复制。 – Pratik

+0

尝试检索查找的项目的ID而不是LookupId,就像我在代码中那样。 – Kilazur

相关问题