The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
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)] |
[ScriptService] |
// 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 |
//InitializeComponent(); |
} |
[WebMethod] |
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) |
{ |
if(content.GetMetaData(metaKey.ToString())!=null) |
newContent.SetMetaData(metaKey.ToString(),content.GetMetaData(metaKey.ToString())); |
} |
//copy content |
newContent.Content = content.Content; |
//copy parent IDs |
if(content.ParentID!=null) |
newContent.ParentID = content.ParentID; |
//copy comments |
if(manager.Provider.AllowComments) |
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; |
manager.SaveComment(newComment); |
} |
//copy item info |
if(content.ItemInfo!=null) |
newContent.ItemInfo = content.ItemInfo; |
//copy thumbnails |
if(content.Thumbnails.Count>0) |
foreach (var thumbnail in content.Thumbnails) |
{ |
try |
{ |
newContent.Thumbnails.Add(thumbnail); |
} |
catch { } |
} |
//save newly copied content |
manager.SaveContent(newContent); |
} |
} |
} |
} |
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) |
{ |
base.OnPreRender(e); |
... |
ScriptManager sm = ScriptManager.GetCurrent(this.Page); |
ServiceReference reference = new ServiceReference("~/Sitefinity/Services/CustomContentService.asmx"); |
reference.InlineScript = true; |
sm.Services.Add(reference); |
} |
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> |
<ul> |
<li> |
<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 %>" /> |
</li> |
<li> |
<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 %>" /> |
</li> |
</ul> |
</div> |
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(); |
$("#disabledDeleteGroupGrid").hide(); |
$("#disabledDuplicateGroupGrid").hide(); |
} |
else { |
$("#<%= btnDeleteGroupGrid.ClientID %>").hide(); |
$("#<%= btnDuplicateGroupGrid.ClientID %>").hide(); |
$("#disabledDeleteGroupGrid").show(); |
$("#disabledDuplicateGroupGrid").show(); |
} |
} |
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" /> |
View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.