Sample Community Server Enhancement
Adonis, 29 Apr 2009 3:35 PMI saw a tweet come across the other day from @adamkranitz:
adamkranitz: I would kill for "Duplicate This Post" button in Telligent CommunityServer blog software.
Hmm… I have a list… I mean, I think I can figure that out…
So I asked @adamkranitz for some details, pulled down the Community Server 2008.5 SDK and got to work.
I’m not a developer and you should probably consider that if you take any code off my blog but here is how I chose to solve the “Duplicate This Post” button need.
First, I’m going to do this directly in the UI of the control panel with some server-side code. I need a “Duplicate” button on the post list page of my site. The URL is http://localhost/CS2008.5SP1/controlpanel/blogs/postlist.aspx. In the SDK, I looked at the contents of \source\CommunityServer.Web\ControlPanel\Blogs and found PostList.aspx and PostListControl.ascx. I made a backup of PostListControl.ascx (an EXTREMELY important step for me…) and took a look.
So – first, I’ve got to add the button. I noticed that there was a alternating item template in the page so I’ve got to add my button in both sections:
<cp:ResourceButton CommandArgument='<%# DataBinder.Eval(Container.DataItem, "PostID")%>' CommandName="Duplicate" OnCommand="Duplicate_Click" Runat="server" ID="DuplicateButton" Text="Duplicate" />
The CommandArgument was already there in the Delete button above where I wanted my Duplicate button so I got lucky.
The hard part of this whole thing was figuring out what attributes needed to be set to get the post to show up. There’s a copy() method on a Post entity but I decided to set each value individually. What I ended up with was the following:
<script runat="server">
protected void Duplicate_Click(object sender, CommandEventArgs e)
{
int PostID;
if (int.TryParse(e.CommandArgument.ToString(), out PostID))
DuplicatePost(PostID);
else
return;
}
private void DuplicatePost(int PostID)
{
WeblogPost orig = WeblogPosts.GetPost(PostID, true, false, true);
//WeblogPost newpost = (WeblogPost)orig.Copy();
WeblogPost newpost = new WeblogPost();
newpost.IsApproved = false;
newpost.Body = orig.Body;
newpost.Subject = "Copy of " + orig.Subject;
newpost.Categories = orig.Categories;
newpost.PostDate = DateTime.Now;
newpost.ThreadDate = DateTime.Now;
newpost.UserTime = DateTime.Now;
newpost.PostType = orig.PostType;
//newpost.ApplicationType = orig.ApplicationType;
newpost.BlogPostType = orig.BlogPostType;
newpost.Name = "Copy of " + orig.Name;
//newpost.ExtendedAttributesKeys = orig.ExtendedAttributesKeys;
newpost.AuthorID = orig.AuthorID;
newpost.Section = orig.Section;
newpost.SectionID = orig.SectionID;
WeblogPosts.Add(newpost, orig.User);
}
</script>
By the way – to get this working, I had to open up SQL and see what was being stored in the CS_Posts table – I tweaked values in the table to get the posts showing up on the page and then fixed the code in my page to do what I knew needed to be done…
A better way to do this would probably have been through REST but that’s documented – where’s the fun in that?!?!
Ok, the file can be downloaded here if you want the whole control.


Leo says:
23 May 2009 at 8:45 PMNice. So, can you share tidbits of what's coming ahead in the new CS? :)
Adonis says:
27 May 2009 at 12:42 PMDuuuude - I'd love to - it's kickin'! But... I can't - not yet...
oes tsetnoc says:
13 Nov 2009 at 1:13 PMGreat blog, this could be the best blog I ever visited this month. Never stop to write something useful dude!