[英]C# checking if SharePoint list entry already exist .. CAML?
I have two identical SharePoint lists. 我有两个相同的SharePoint列表。 I'm using C# to loop through one and adding to the other using the SP object model.
我正在使用C#遍历一个对象,并使用SP对象模型将其添加到另一个对象中。
Problem is I don't want to add to the other, if there is already a list entry with 3 specific fields that match the from list. 问题是,如果已经有一个包含3个与from列表匹配的特定字段的列表条目,我不想添加到另一个列表中。
How can do this is in C#? 如何在C#中做到这一点? using CAML?
使用CAML?
Say my lists are called from and to and the fields are called a,b and c. 假设我的清单是从and到的,而栏位是a,b和c。
Here, I don't want to add a entry to "to", if there is already an entry in to with a,b and c matching my currently from entry. 在这里,如果已经有一个与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
I would start by using SPMetal (LINQ To SharePoint if possible). 我将首先使用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
}
The other way is like you mentioned and use SPQuery. 另一种方法就像您提到的并使用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
I am not at my normal PC so I can't test either of these codes but should give you an idea of how to start, it is unfortunate that SharePoint doesn't allow composite unique keys. 我不在普通PC上,因此我无法测试这两个代码,但应向您提供如何启动的想法,很遗憾,SharePoint不允许使用复合唯一键。
It would be possible to enforce the composite unique key with an event receiver on that list if you want to approach the problem from a different angle. 如果要从另一个角度解决问题,则可以使用该列表上的事件接收器来强制执行复合唯一键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.