我正在SharePoint中编写javascript(CSOM)代码,以便将项目添加到SharePoint日历应用程序(列表)。
我想仅在日期在指定时间内免费添加这些记录。
即在下面的例子中,我想在添加
之前检查日历是否从08:00到08:15免费 var itemCreateInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(itemCreateInfo);
var startdate = new Date('2017-10-21 08:00 GMT+0000').toISOString();
var enddate = new Date('2017-10-21 08:15 GMT+0000').toISOString();
listItem.set_item("Title", "My Event");
listItem.set_item("EventDate", startdate);
listItem.set_item("EndDate", enddate);
listItem.set_item("Location", "MyCalander");
listItem.update();
答案 0 :(得分:0)
这是您可以使用的查询:(caml值看起来很糟糕,因此我将在下面发布xml)
function queryCalendar(startDate, endDate) {
var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle("Calendar");
var caml = "\n<View>\n <Query>\n <Where>\n <And>\n <Lt>\n <FieldRef Name='EventDate' />\n <Value Type='DateTime' IncludeTimeValue='True' StorageTZ='TRUE'>" + endDate.toISOString() + "</Value>\n </Lt>\n <Gt>\n <FieldRef Name='EndDate' />\n <Value Type='DateTime' IncludeTimeValue='True' StorageTZ='TRUE'>" + startDate.toISOString() + "</Value>\n </Gt>\n </And>\n </Where>\n </Query>\n <ViewFields>\n <FieldRef Name='EventDate' />\n <FieldRef Name='EndDate' />\n <FieldRef Name='Title' />\n </ViewFields>\n</View>\n\n";
var query = new SP.CamlQuery();
query.set_viewXml(caml);
var listItems = list.getItems(query);
context.load(listItems);
context.executeQueryAsync(function (sender, args) {
if (listItems.get_count() > 0) {
console.warn("That time is already booked");
}
else {
console.log("that time is good");
}
}, function (sender, args) {
console.error(args.get_message());
});
}
这是我的称呼方式:
SP.SOD.executeOrDelayUntilScriptLoaded(function () {
queryCalendar(new Date(2019, 4, 27, 8), new Date(2019, 4, 27, 9));
}, "SP.js");
我将以下XML用于CAML:
<View>
<Query>
<Where>
<And>
<Lt>
<FieldRef Name='EventDate' />
<Value Type='DateTime' IncludeTimeValue="True" StorageTZ='TRUE'>enddate</Value>
</Lt>
<Gt>
<FieldRef Name='EndDate' />
<Value Type='DateTime' IncludeTimeValue="True" StorageTZ='TRUE'>starttime</Value>
</Gt>
</And>
</Where>
</Query>
<ViewFields>
<FieldRef Name='EventDate' />
<FieldRef Name='EndDate' />
<FieldRef Name='Title' />
</ViewFields>
</View>
请确保使用正确的日期值替换“ enddate”和“ startdate”,并使用date.toISOString()方法(在IE中可能无法使用)。