代码改变世界

MOSS2010 客户端对象模型开发(三)

2012-08-31 14:00  温森特  阅读(662)  评论(1编辑  收藏  举报
本篇将讲述MOSS2010客户端对象模型操作MOSS里面的讨论列表,讨论列表是一个特殊的列表,因此对它的操作就不能像普通的列表那样操作了,否则会产生一系列的问题的。首先我先讲述一下讨论列表的添加操作,看如下代码,向一个讨论列表里面增加一条讨论主题:

 

                using (ClientContext client = new ClientContext(Url))
                {
                    client.Credentials = new NetworkCredential(UserName, UserPassword, Domain);
                    Web web = client.Web;
                    client.Load(web);
                    //获得列表
                    List list = web.Lists.GetById(new Guid(ThreadID));
                    client.Load(list);

                    //添加帖子
                    ListItem listItem = Utility.CreateNewDiscussion(client, list, Title);
                    listItem["Body"] = Content;
                    listItem.Update();
                    client.Load(listItem);

                    client.ExecuteQuery();         
                }


读取讨论板列表的主题,以及对该主题进行的评论回复信息:

 using (ClientContext clientContext = new ClientContext(url))
                {
                    clientContext.Credentials = new NetworkCredential(UserName, UserPassword, Domain);
                    Web web = clientContext.Web;
                    clientContext.Load(web);

                    List list = web.Lists.GetById(new Guid(listGuid));
                    clientContext.Load(list);


                    //获取标题内容
                    ListItem itemThread = list.GetItemById(itemId);               
                    clientContext.Load(itemThread);
          
                    DataRow rowThread = dt.NewRow();
                    rowThread["ID"] = itemThread["ID"] == null ? "" : list.Id + "^" + itemThread["Title"].ToString();
                    rowThread["ClassName"] = list.Title == null ? "" : list.Title;
                    rowThread["Title"] = itemThread["Title"] == null ? "" : itemThread["Title"].ToString();
                    rowThread["Author"]=(itemThread.FieldValues["Author"as FieldUserValue).LookupValue;
                    rowThread["UpdateTime"] = Convert.ToDateTime(itemThread["Modified"].ToString()).ToLocalTime();
                    rowThread["Contents"] = itemThread["Body"] == null ? "" : itemThread["Body"].ToString();
                    dt.Rows.Add(rowThread);
                    
                    // 获取回复内容                   
                    CamlQuery camlReply = new CamlQuery();
                    camlReply.FolderServerRelativeUrl = itemThread["FileRef"].ToString();
                    camlReply.ViewXml = "<View></View>";

                    ListItemCollection ReplylistItemCollection = list.GetItems(camlReply);
                    clientContext.Load(ReplylistItemCollection);

                    if (ReplylistItemCollection != null)
                    {

                        foreach (ListItem item in ReplylistItemCollection)
                        {
                            DataRow row = dt.NewRow();
                            row["ID"] = item["ID"] == null ? "" : item["ID"].ToString();
                            row["ClassName"] = s[1];
                            row["Title"] = item["TrimmedBody"] == null ? "" : item["TrimmedBody"].ToString();
                            row["Author"]=(item.FieldValues["Author"as FieldUserValue).LookupValue;
                            row["UpdateTime"] = Convert.ToDateTime(item["Modified"].ToString()).ToLocalTime();
                            row["Contents"] = item["Body"] == null ? "" : item["Body"].ToString();
                            dt.Rows.Add(row);
                        }

     }

 

     clientContext.ExecuteQuery(); 

                 }  

 

  对讨论主题进行评论,即对讨论发布评论:



using (ClientContext clientContext = new ClientContext(Url))
                {
                    clientContext.Credentials = new NetworkCredential(UserName, UserPassword, Domain);               
                    Web web = clientContext.Web;
                    clientContext.Load(web);
                    //获得列表
                    List list = web.Lists.GetById(new Guid(listGuid));
                    clientContext.Load(list);

                    ListItem tempItem = list.GetItemById(itemId);
                    ListItem newListItem = Utility.CreateNewDiscussionReply(clientContext, tempItem);
            
                    newListItem["Body"] = Content;
                    newListItem.Update();
                    
                    clientContext.Load(newListItem);
                    clientContext.ExecuteQuery();  

                }