MOSS2010的开发框架增加了客户端对象模型,增加了MOSS开发的灵活性,对于一些特殊需求的地方,非常的灵活。相对于MOSS服务器端开发,还是有一些值得注意的地方,我将会分几次对MOSS客户端开发技巧进行讲解。
MOSS2010 客户端开发,首先要得到网站的上下文,一般我们需要当前登录人的一些验证信息,如用户名和密码确认身份后,得到网站的上下文。
ClientContext client = new ClientContext(Url)
NetworkCredential credentials = new NetworkCredential(UserName, UserPassword,Domain));
client.Credentials = credentials;
然后就像服务器端那样开发,但是每当获取一个对象的时候,都要用上下进行加载操作, client.Load(……);否则对象本身实际上是没有被加载,得到的将是NULL。当所有的代码都执行完的时候,统一进行提交, client.ExecuteQuery(); 这个时候,才是真正的提交到服务器上进行执行。
下面是演示代码:
using (ClientContext client =
new ClientContext(Url))
{
NetworkCredential credentials = new NetworkCredential(UserName, UserPassword, Functions.GetAppSeetingValueByKey(SysNames.P_DOMAIN_NAME));
client.Credentials = credentials;
Web web = client.Web;
client.Load(web);
List addressList = client.Web.Lists.GetByTitle( " XXXXX ");
client.Load(addressList);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @" <View><Query><OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy></Query><RowLimit>1</RowLimit></View> ";
ListItemCollection listItems = addressList.GetItems(camlQuery);
client.Load(listItems);
client.ExecuteQuery();
if (listItems.Count > 0)
{
resutl = DateTime.Parse(listItems[ 0][ " Modified "].ToString()).ToString( " yyyy-MM-dd HH:MM:ss ");
}
}
{
NetworkCredential credentials = new NetworkCredential(UserName, UserPassword, Functions.GetAppSeetingValueByKey(SysNames.P_DOMAIN_NAME));
client.Credentials = credentials;
Web web = client.Web;
client.Load(web);
List addressList = client.Web.Lists.GetByTitle( " XXXXX ");
client.Load(addressList);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @" <View><Query><OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy></Query><RowLimit>1</RowLimit></View> ";
ListItemCollection listItems = addressList.GetItems(camlQuery);
client.Load(listItems);
client.ExecuteQuery();
if (listItems.Count > 0)
{
resutl = DateTime.Parse(listItems[ 0][ " Modified "].ToString()).ToString( " yyyy-MM-dd HH:MM:ss ");
}
}
下一章节,我将讲述MOSS客户端对特殊的一些列表的访问和操作。