在你的项目中,你可能需要用CSOM去对列表进行增删改查。下面就让我来介绍一下这个基本过程以及自己在项目开发中遇到的问题和解决的办法。由于本人也是在学习中,故如果不当,请予以指出。谢谢。如果需要CSOM的Api,可进入http://msdn.microsoft.com/en-us/library/office/dn268594.aspx查看。
首先,在是使用CSOM的时候,我们一定会用到的类ClientContext。
ClientContext ctx = new ClientContext(spsiteurl)
其次,如果你运行的环境并不在sharepoint的场服务器中,则需要设置身份验证。ClientContext的AuthenticationMode属性可以让我们设置身份验证的类别。有3中,分别是 Default = 0,FormsAuthentication = 1,Anonymous = 2。其中Default指的Windows 身份验证,FormsAuthentication是表单,Anonymous是匿名。
假设你的Sharepoint WebApplication配置的是单单支持Windows身份验证,那么你只需要如下输入就可以完成身份验证。
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
NetworkCredential Credentials = new NetworkCredential(adminloginname, adminpassword);
ctx.Credentials = Credentials;
如果你的Sharepoint WebApplication配置的是单单支持表单身份验证,则如下输入
ctx.AuthenticationMode=ClientAuthenticationMode.FormsAuthentication;
ctx.FormsAuthenticationLoginInfo=new FormsAuthenticationLoginInfo("loginname","password");
如果你的Sharepoint WebApplication配置的是同时支持表单身份验证和Windows身份验证,则在你使用表单账号登陆的时候依旧如上所示。
但是假如你想使用windows的账号来登陆,则需要做如下处理
ctx.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(clientContext_ExecutingWebRequest);
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
NetworkCredential Credentials = new NetworkCredential(adminloginname, adminpassword);
ctx.Credentials = Credentials;
public static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
{
//禁用掉Form身份验证
e.WebRequestExecutor.WebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
}
这样,我们就能使用windows 的账号进行登陆。
得到了当前ClientContext对象之后,我们可以获得当前的Web以及List
Web site = ctx.Web;
List list = site.Lists.GetByTitle(itemtitle);
List summarylist = site.Lists.GetByTitle(summarytitle);
ctx.Load(site);
ctx.Load(list);
ctx.Load(summarylist);
ctx.ExecuteQuery();
这样,我们就可以对所获得的List添加一条数据(包括附件)
CSOM添加数据并不像server Code一样,我们在这里需要先生成一个ListItemCreationInformation对象,并添加到List对象中。
ListItemCreationInformation listItemCI = new ListItemCreationInformation();
ListItem item = list.AddItem(listItemCI);
item["Title"]="xxx";
item.Update();
ctx.ExecuteQuery();
然后再得到的ListItem对象中设置各个Field的指,最后调用Update方法即可。
下面来介绍附件的添加。如下是附件添加的代码,当然这个前提是这个Item中尚且还没有附件
AttachmentCreationInformation attachmentcreation = new AttachmentCreationInformation();
attachmentcreation.FileName = filename;//文件名
attachmentcreation.ContentStream = new MemoryStream(attachmentsbytes.Skip(count +4 + filenamecount + 4).Take(filebytescount).ToArray());//流对象
item.AttachmentFiles.Add(attachmentcreation);
如果这个Item中已经有了附件,如果再添加附件,则需要换一种方法。这样附件就完美的添加上去了。
if (item["Attachments"].ToString() == "True")//判断有无附件
{
//这个是在当前item已经有附件的情况下才能使用上传
string attachmentpath = @"/" + spweburl + "/Lists/" + listname + "/Attachments/" + item.Id + @"/" + filename;
Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, attachmentpath, memory, true);//这个true表示如果存在同名的文件,则强制复制
}
else
{
AttachmentCreationInformation attachmentcreation = new AttachmentCreationInformation();
attachmentcreation.FileName = filename;
attachmentcreation.ContentStream = memory;
item.AttachmentFiles.Add(attachmentcreation);
ctx.ExecuteQuery();
}
接下来我们介绍一下查询操作。在CSOM中,我们主要使用Caml来查询数据。
下面是一个简单例子
CamlQuery query = new CamlQuery();
query.ViewXml = string.Format(
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='Type' />
<Value Type='Text'>{0}</Value>
</Eq>
</Where>
<OrderBy>
<FieldRef Name='PublishTime' Ascending='FALSE' />
</OrderBy>
</Query>
</View>", notificationtyep);
ListItemCollection items = list.GetItems(query);
ctx.Load(items);
ctx.ExecuteQuery();
这样我们就能获取到符合条件的数据,当然这个仅仅是最简单的查询,还有其他一些比较复杂的,例如join等等。join操作我将在下一篇详细介绍。
谢谢您的光临。