• Working with SharePoint’s Discussion Lists Programmatically – Part 2


    Working with SharePoint’s Discussion Lists Programmatically – Part 2

    Posted Wednesday, May 05, 2010 3:43 PM by Itay Shakury

    This 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.

  • 相关阅读:
    OCP-1Z0-053-V12.02-235题
    OCP-1Z0-053-V12.02-524题
    OCP-1Z0-053-V12.02-525题
    OCP-1Z0-053-V12.02-526题
    OCP-1Z0-053-V12.02-535题
    OCP-1Z0-053-V12.02-540题
    OCP-1Z0-053-V12.02-617题
    OCP-1Z0-053-V12.02-649题
    如何制作Jar包并在android中调用jar包
    JAVA实现回调
  • 原文地址:https://www.cnblogs.com/ahghy/p/2780445.html
Copyright © 2020-2023  润新知