Try Now
More in this section
Blogs RSS feed

Use the Content Manager API in a Web Service

by Radoslav Georgiev
This post as my previous post is inspired by a client support request to extend Sitefinity functionality in a way. I also think that this is a good way to sample how the modules API can be used in web services. Following the logic of group operations functionality added in Sitefinity 3.7 we can now create a simple web service to allow us to duplicate content items in modules. For more information on group operations, please follow this link.

For this example I will be using Sitefinity 3.7 because it already provides some ready scripts that help us get the selected item IDs and pass them to the web service. The use of the bellow sample is also achievable in older versions of Sitefintiy, however there you will need to add new button to the grid containing content items and then subscribe to its click event.

Now lets start by creating a regular web service in your Sitefinity website. I have created one in the ~/Sitefinity/Services folder and named the file CustomContentService.asmx. When you create a web service VS automatically generates the code file in your ~/App_Code directory. Bellow is the sample code that we will use to create copies of content items:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using Telerik.Cms.Engine; 
using Telerik.Security.Permissions; 
using System.Web.Script.Services; 
namespace Telerik.Samples.WebServices 
    /// <summary> 
    /// Summary description for CustomContentService 
    /// </summary> 
    [WebService(Namespace = "Telerik.Samples.WebServices")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.  
    // [System.Web.Script.Services.ScriptService] 
    public class CustomContentService : System.Web.Services.WebService 
        public CustomContentService() 
            //Uncomment the following line if using designed components  
        public void DuplicateContent(Guid[] contentIDs, string providerName) 
            ContentManager manager = new ContentManager(providerName); 
            //create copies of selected content items 
            foreach (Guid guid in contentIDs) 
                manager.GetPermission(guid, CrudRights.Create).Demand(); 
                //get content to be copied 
                IContent content = manager.GetContent(guid); 
                //create new content 
                IContent newContent = manager.CreateContent(content.MimeType); 
                //copy metafields 
                foreach (var metaKey in manager.Provider.MetaKeys.Keys) 
                //copyt content 
                newContent.Content = content.Content; 
                //copy parrent IDs 
                    newContent.ParentID = content.ParentID; 
                //copy comments 
                    foreach (IComment comment in content.Comments) 
                        IComment newComment = manager.CreateComment(newContent); 
                        newComment.Author = comment.Author; 
                        newComment.Email = comment.Email; 
                        newComment.IpAddress = comment.IpAddress; 
                        newComment.Owner = newComment.Owner; 
                        newComment.Visible = comment.Visible; 
                        newComment.Text = comment.Text; 
                        newComment.WebSite = comment.WebSite; 
                //copy item info 
                    newContent.ItemInfo = content.ItemInfo; 
                //copu thumbnails 
                    foreach (var thumbnail in content.Thumbnails) 
                        catch { } 
                //save newly copied content 


What we need to do now is to register the newly created web service to the script manager, so that we can use its methods. You can do this in the OnPreRender event handler of the ~/Sitefinity/Admin/Modules.aspx.cs:

protected override void OnPreRender(EventArgs e) 
    ScriptManager sm = ScriptManager.GetCurrent(this.Page); 
    ServiceReference reference = new ServiceReference("~/Sitefinity/Services/CustomContentService.asmx"); 
    reference.InlineScript = true


Next we need to add some markup and JS to the control templates for displaying content items in the modules where we need this service. For this you will need to work with the external templates for your version of Sitefinity. Here I will sample how to do this with the Generic Content module. Open the ~/Sitefinity/Admin/ControlTemplates/Generic_Content/ContentItemsList.ascx. First add a link button to the div that holds group operations commands:

<div class="groupActionsPopup"
    <span class="numberOfSelectedItem"><strong id="numberOfSelectedItems">0</strong><asp:Literal ID="lblSelectedItems" runat="server" Text="<%$Resources:ItemsSelected %>"></asp:Literal></span
            <span id="disabledDeleteGroupGrid" class="disabled"><asp:Literal ID="Literal20" runat="server" Text="<%$Resources:Delete %>"></asp:Literal></span>  
            <asp:LinkButton Style="display: none;" ID="btnDeleteGroupGrid" runat="server" OnClientClick="DeleteWithConfirmation(); return false;" Text="<%$Resources:Delete %>"></asp:LinkButton> 
            <asp:Literal ID="lblSelected" runat="server" Text="<%$Resources:Selected %>" /> 
            <span id="disabledDuplicateGroupGrid" class="disabled"><asp:Literal ID="Literal6" runat="server" Text="Duplicate"></asp:Literal></span>  
            <asp:LinkButton Style="display: none;" ID="btnDuplicateGroupGrid" runat="server" OnClientClick="DuplicateSelected(); return false;" Text="Duplicate"></asp:LinkButton> 
            <asp:Literal ID="Literal1" runat="server" Text="<%$Resources:Selected %>" /> 

Then add some JS to be make the button look and behave as the rest of the commands:

function EnableOrDisableGroupOperationButtons(enable) { 
    if (enable) { 
        $("#<%= btnDeleteGroupGrid.ClientID %>").show(); 
        $("#<%= btnDuplicateGroupGrid.ClientID %>").show(); 
    else { 
        $("#<%= btnDeleteGroupGrid.ClientID %>").hide(); 
        $("#<%= btnDuplicateGroupGrid.ClientID %>").hide(); 


Then add a java script function that will handle the on client click event of the duplicate button that we have just added. This function will make the call to our web service passing the selected item IDs:

function DuplicateSelected() { 
    var guidsToPass = []; 
    for (var i = 0; i < selectedContentIDs.length; i++) { 
        if (selectedContentIDs[i] != undefined) { 
            guidsToPass[guidsToPass.length] = selectedContentIDs[i]; 
    //we are using similar logic as in the group operations functionality 
    //call the webservice and rebind grid 
    Telerik.Samples.WebServices.CustomContentService.DuplicateContent(guidsToPass, dataProviderName, DeleteContent_Success, OnFailed); 

All you have to do now is to map the control template in your ControlsConfig file and restart the website:

<!--Provides user interface for displaying a list of content items in the Generic Content module.--> 
<viewSettings hostType="Telerik.Cms.Engine.WebControls.Admin.ContentItemsList`1[[Telerik.Cms.Engine.WebControls.Admin.ContentItemsView, Telerik.Cms.Engine]]" layoutTemplatePath="~/Sitefinity/Admin/ControlTemplates/Generic_Content/ContentItemsList.ascx" /> 


Leave a comment
  1. Haddy Dec 15, 2009
    Thanks for the information, I think this would be great to implement... Unfortunatly when I try to implement it with 3.7 SP2, I get an error claiming "The resource object with key 'GroupOperations' was not found."
  2. Radoslav Georgiev Dec 23, 2009
    Thank you for the interest. Can you, please make sure that you have added the localization resources for each template edited to the App_LocalResoures directory, contained in the same folder as your control template. For example if you are working with ~/Sitefinity/Admin/ControlTemplates/Generic_Content/ContentItemsList.ascx, make sure that you have ~/Sitefinity/Admin/ControlTemplates/Generic_Content/App_LocalResoures/ContentItemsList.ascx.resx file. Open this file for edit and see if it has an entry for GroupOperations. If you do not have this file you can find it in the external templates archive

    Leave a comment