Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Umbraco change templates on multiple documents
using System;
using System.Web;
using System.Web.Services;
using System.Collections;
using System.Web.Services.Protocols;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.web;
using umbraco.BusinessLogic;
using System.Xml.Serialization;
using umbraco.cms.businesslogic.property;
using umbraco.cms.businesslogic.macro;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class YourWebservice : System.Web.Services.WebService
{
[WebMethod]
public void UpdateDocumentParent(int docId, int parentId, bool deep, int templateId)
{
if (deep)
{
ArrayList list = this.GetDocumentChildren(docId);
for (int i = 0; i < list.Count; i++)
{
if (list[i] is Document)
{
int n;
int.TryParse(((Document)list[i]).Id.ToString(), out n);
if (n > 0)
{
try
{
Document d = new Document(n);
d.Publish(new User(0));
}
catch { }
}
}
}
}
else
{
Document d = new Document(docId);
d.Template = templateId;
d.Publish(new User(0));
}
}
private ArrayList GetDocumentChildren(int docId)
{
Document doc = new Document(docId);
Document[] children = doc.Children;
ArrayList list = new ArrayList();
if (children.Length > 0)
{
for (int i = 0; i < children.Length; i++)
{
if (children[i].HasChildren)
{
list.AddRange(GetDocumentChildren(children[i].Id));
}
list.Add(children[i]);
}
}
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.