Working with SharePoint’s Discussion Lists Programmatically – Part 2
2012-11-21 11:47
549 查看
Working with SharePoint’s Discussion Lists Programmatically – Part 2
Posted Wednesday, May 05, 2010 3:43 PM by Itay ShakuryThis is part of a series of posts about Working with Discussion lists programmatically: Part 1, Part 2 (this one), Part 3.
In Part 1, I have introduced the structure and inner workings of SharePoint Discussion Lists. Now, Lets take a look of some code samples with SharePoint’s Object Model, and LINQ 2 SharePoint.
Getting all Posts
ForumDataContext dc = new ForumDataContext("http://dev-sp2010-01/sites/itaysk/Forum"); //Get all topics in the Discussion list IEnumerable<Discussion> topics = from t in dc.TeamDiscussion select (Discussion)t;
We ask for all the list items that are in the root of the list. Those are essentially the items of type “Discussion” which are folders.
Alternatively, you can use SPList.GetItems(SPQuery query) to get the items. An empty query will do because the list contains only topics at it’s root. (You could filter for Folder content type if you like too, but it’s not necessary)
SPList list = web.Lists["Team Discussion"]; SPListItemCollection res = list.GetItems(new SPQuery());
Getting all Replies for a post
//select a single discussion (in this case, the first one), to view it's content Discussion topic = (Discussion)dc.TeamDiscussion.Single(t => t.Id == 5); //Get all the replies for the selected discussion IEnumerable<Message> replies = from reply in dc.TeamDiscussion.ScopeToFolder("/"+topic.Reply+"/"+topic.Title, false) select (Message)reply;
After selecting a specific post (folder), we are asking for all the list items that are in that folder.
Alternatively, if you prefer to work with CAML, you can use this CAML query that asks for all items in a folder:
SPList list = web.Lists["Team Discussion"]; //Get the topic. (you can use other ways to get the topic) SPFolder t = list.GetItemById(1).Folder; SPQuery q = new SPQuery(); q.Folder = t; SPListItemCollection res = list.GetItems(q);
Or use this one, that uses the “ParentFolderId” column that every reply has.
SPList list = web.Lists["Team Discussion"]; //This Query gets all items of the topic with ID=1 string strQ = @"<Query> <Where> <Eq> <FieldRef Name=""ParentFolderId"" /> <Value Type=""Integer"">1</Value> </Eq> </Where> </Query>"; SPQuery q = new SPQuery(); //This line makes the query search ib all folders q.ViewAttributes = "Scope=\"Recursive\""; q.Query = strQ; SPListItemCollection res = list.GetItems(q);
Creating a Topic
Don’t be tempted to manually create a regular list item in the list, because it will miss Threading and other stuff the mechanism needs.
Luckily, we have a special function that does all this for us.
SPList list = web.Lists["Team Discussion"]; SPListItem t = Microsoft.SharePoint.Utilities.SPUtility.CreateNewDiscussion( list, "Created by Code"); t[SPBuiltInFieldId.Body] = "Created by Code"; t.Update();
Creating a Reply
Again, using the proprietary function.
SPList list = web.Lists["Team Discussion"]; //Get the topic for which we are replying to. (you can also get it in other ways) SPListItem t = list.GetItemById(11); SPListItem r = Microsoft.SharePoint.Utilities.SPUtility.CreateNewDiscussionReply( t); r[SPBuiltInFieldId.Body] = "Created by Code"; r.Update();
In this example, we have replied to the root of the topic. You can also reply to a specific reply inside the topic – just pass the CreateNewDiscussionReply function the object that you want to reply to.
Conclusion
In this post we learned how to create discussion items, and replies, as well as query them. We have used Server Object Model.
In the next posts I will show how to do the same things from the client.
相关文章推荐
- Working with SharePoint’s Discussion Lists Programmatically – Part 1
- Working with SharePoint’s Discussion Lists Programmatically – Part 3
- Personalize a SharePoint Site with Audiences - Part 1
- Treeview Navigation Web Part for SharePoint with SmartPart v1.0
- [转]Working with user roles and permissions in SharePoint Object Model
- SharePoint Designer Tutorial: Working with SharePoint Websites
- Tab with web parts from web part zone in Sharepoint 2013
- Visual guide to Windows Live ID authentication with SharePoint 2010 - part 1
- 好文推荐:Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0
- Working with user roles and permissions in SharePoint Object Model
- 好文推荐:Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0
- Using the SharePoint 2010 Client Object Model_Part_6
- 调试SharePoint web part时, 如何可以多个人同时启动调试?
- In-Place upgrade to Team Foundation Server (TFS) 2015 from TFS 2013Team Foundation Server TFS TFS 2015 TFS upgrade TFS with Sharepoint
- 利用AutoSPSourceBuilder和Autospinstaller自动安装SharePoint Server 2013图解教程——Part 2
- 调试SharePoint web part时, 如何可以多个人同时启动调试?
- Session state cannot be used in ASP.NET with Windows SharePoint Services
- Label Web Part for SharePoint Portal Server 2003 [Source Codes in C#]
- SharePoint UI 定制 (原创)Part 2
- MOSS应用场景之一-http://sharepoint.microsoft.com/sharepoint/worldwide/cn/IW/Lists/Announcements/DispForm.aspx?ID=4