More in this section
Blogs RSS feed

Use the Content Manager API in a Web Service

by User Not Found
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 Sitefinity, 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) 
                //copy content 
                newContent.Content = content.Content; 
                //copy parent 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; 
                //copy 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
  3. Emma120 May 15, 2018
    This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck!
    HP Support Number
  4. Karen Jun 10, 2018
    Easy loans for people on benefits are online available loans for the customers so you can apply for these loans from your home if you PC with internet connection.. There are many types of loans out there and using them for the right reasons makes a difference.. Every lender has several loan plans and they float them in the market to differentiate their financial products from the products of the other lenders.. radiant cash personal loans no credit check
  5. Mike Morales Jul 13, 2018

    Really appreciate your help, thanks for sharing, what i was looking for.

    If you are also facing <a href="">hp printer offline windows 10</a> error, just let me help you.

    Leave a comment