using (ClientContext clientContext = new ClientContext("https://cnblogtest.sharepoint.com"))
{
var pasword = new SecureString();
"abc123!@#".ToCharArray().ToList().ForEach(pasword.AppendChar);
clientContext.Credentials = new SharePointOnlineCredentials("test001@cnblogtest.onmicrosoft.com", pasword);//设置权限
var currentWeb = clientContext.Web;
var file = currentWeb.GetFileByServerRelativeUrl("/Shared%20Documents/Sharepoint.png");
file.ListItemAllFields["Test"] = "StartIfTrue";
file.ListItemAllFields.Update();
clientContext.ExecuteQuery();//提交修改,如果文件不存在会出异常
}
using (ClientContext clientContext = new ClientContext("https://cnblogtest.sharepoint.com"))
{
var pasword = new SecureString();
"abc123!@#".ToCharArray().ToList().ForEach(pasword.AppendChar);
clientContext.Credentials = new SharePointOnlineCredentials("test001@cnblogtest.onmicrosoft.com", pasword);//设置权限
var currentWeb = clientContext.Web;
var file = currentWeb.GetFileByServerRelativeUrl("/Shared%20Documents/Sharepoint1.png");
var conditionalScope = new ConditionalScope(clientContext, () => file.Exists, true);
using (conditionalScope.StartScope())
{
file.ListItemAllFields["Test"] = "StartIfTrue";
file.ListItemAllFields.Update();
}
clientContext.ExecuteQuery();//提交修改,如果文件不存在也不会出异常
if (conditionalScope.TestResult.HasValue)
{//显示ConditionalScope条件的值
Console.WriteLine("Condition Value:" + conditionalScope.TestResult.Value);
}
}
这种方式很好的实现了一次请求中,实现条件判断。请求报文和ExceptionHandlingScope的构成基本类似,这里不再赘述。合理的使用这个类也能很好的提升和SharePoint进行交互时的效率。