繁体   English   中英

C#检查SharePoint列表条目是否已经存在.. CAML?

[英]C# checking if SharePoint list entry already exist .. CAML?

我有两个相同的SharePoint列表。 我正在使用C#遍历一个对象,并使用SP对象模型将其添加到另一个对象中。

问题是,如果已经有一个包含3个与from列表匹配的特定字段的列表条目,我不想添加到另一个列表中。

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

假设我的清单是从and到的,而栏位是a,b和c。

在这里,如果已经有一个与a,b和c匹配我当前from条目的条目,我不想在“ to”中添加一个条目。

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  

我将首先使用SPMetal(如果可能,请使用LINQ To 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不允许使用复合唯一键。

如果要从另一个角度解决问题,则可以使用该列表上的事件接收器来强制执行复合唯一键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM