2012-08-08 145 views
0

我有两个相同的SharePoint列表。我使用C#循环遍历一个,并使用SP对象模型添加到另一个。C#检查SharePoint列表项是否已经存在.. CAML?

问题是我不希望添加到其他的,如果已经有一个列表条目相匹配的从列表3个特定字段。

如何能做到这一点是在C#?使用CAML?

说我的表被并称为和字段名为a,b和c。

在这里,我不想一个条目添加到“到”,如果已经有在用A,B和C匹配我目前从条目的条目。

SPList fList = web.GetList("/sites/xxxx/Lists/from"); 
SPList tList = web.GetList("/sites/xxxx/Lab/Lists/to"); 

foreach (SPListItem fListItem in fList.Items)  
{ 
    // my caml code here i suspect? 
    //SPquery.query = "CAML"; 
    //SPList<yourlist>.GetItems(SPQuery object) 

    SPListItem tListItem = tList.Items.Add();  
    foreach (SPField field in fList.Fields)  
    {     
try 

回答

1

我开始使用SPMetal(如果可能,LINQ到SharePoint)。

MyContext context = new MyContext(SPContext.Current.Web.Url); 

var fItems = from f in context.FromList 
     select f; 

foreach(FItem fitem in fItems) 
{ 
    var toFoundItems = from t in context.ToList 
       where t.Field1 == fitem.Field1 && t.Field2 == fitem.Field2 && t.Field3 == fitem.Field3 
       select t; 

    if(t.Count > 0) 
     continue; 
    else 
     //Code to add items can use context to do this here also 
    } 

另一种方式是像你提到和使用SPQuery。

SPQuery query = new SPQuery(); 
query.Query = string.format(" 
<Where> 
    <AND> 

     <Eq> 
      <FieldRef Name='Field1' /> 
      <Value Type='Text'>{0}</Value> 
     </Eq> 
     <And> 
      <Eq> 
       <FieldRef Name='Field2' /> 
       <Value Type='Text'>{1}</Value> 
      </Eq> 
      <Eq> 
       <FieldRef Name='Field3' /> 
       <Value Type='Text'>{2}</Value> 
      </Eq> 
     </And> 
    </And> 
</Where>"); 

    SPListItemCollection items = tList.GetItems(query); 

if(items.Count > 0) 
    continue; 
else 
    //Code to add item 

我不是在我的正常PC,所以我无法测试这两种代码,但应该给你如何开始的想法,但不幸的是,的SharePoint不允许复合唯一键。

如果您想从不同的角度来处理问题,可以使用该列表上的事件接收器强制执行组合唯一键。

+0

谢谢。这意味着我们在SP2007上,所以我认为没有LINQ。 – o365spo 2012-08-08 15:00:58

+0

我已经问了一个相关的问题。可以从SPD生成的SPDatasource中获得一个计数,并使用C#生成一个过滤器生成CAML?最简单的方法是什么?谢谢。 – o365spo 2012-08-08 18:23:52

+0

是否存在某种使用CAML内部单个qoutes的问题,如下所示..我在此查询中返回每一行时,我应该只返回1行 o365spo 2012-08-09 21:16:37

相关问题