我在SharePoint中创建了带有C#编码的事件接收器。当项目应该匹配时已经有了。它显示这样的按摩是我的代码..
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
SPList oList = null;
string strCarName = string.Empty;
using (SPWeb web = properties.Web)
{
oList = web.Lists["Cars"];
string dropdwnvalue = web.Lists[properties.ListId].Fields["CarName"].InternalName;
String lookupFieldStringValue = Convert.ToString(properties.AfterProperties[dropdwnvalue]);
SPListItem item = oList.Items[Convert.ToInt32(lookupFieldStringValue) - 1];
strCarName = Convert.ToString(item["LinkTitle"]);
}
string strStartdate = Convert.ToString(properties.AfterProperties["EventDate"]);
SPQuery existingItemsQuery = new SPQuery();
existingItemsQuery.Query = "<Where><And><Eq><FieldRef Name='EventDate'/><Value Type='DateTime'>" + strStartdate + "</Value></Eq><Eq><FieldRef Name='Car_x0020_Name'/><Value Type='Lookup'>" + strCarName + "</Value></Eq></And></Where>";
SPListItemCollection existingItems = properties.List.GetItems(existingItemsQuery);
if (existingItems.Count >= 1)
{
properties.Cancel = true;
properties.ErrorMessage = "Item is already exists";
properties.Status = SPEventReceiverStatus.CancelWithError;
// ClientScript.RegisterStartupScript(typeof(Page), "test", "<script>alert('Hello');return false;</script>");
}
输出: -
项目已存在。 该项只在一天中添加一次。我不能再添加另一项。但我想在一天的更改时间内添加另一个项目
任何人都可以帮助我。
答案 0 :(得分:1)
正如我所看到的,您对查询字符串有疑问,您应该使用:
existingItemsQuery.Query = "<Where><And><Eq><FieldRef Name='EventDate'/><Value Type='DateTime' IncludeTimeValue='TRUE'>" + strStartdate + "</Value></Eq><Eq><FieldRef Name='Car_x0020_Name'/><Value Type='Lookup'>" + strCarName + "</Value></Eq></And></Where>";
你应该使用strStartdate这样的格式'1971-01-01T00:00:00Z'
您可以使用此方法:
SPUtility.CreateISO8601DateTimeFromSystemDateTime(properties.AfterProperties["EventDate"])
希望这有帮助!