<%@ Webservice class="umbSimpleWS" %> using System; using System.Data; using System.Configuration; using System.Reflection; using System.Web; using System.Web.Services; using System.Xml; using System.Xml.XPath; using umbraco.cms.businesslogic.web; [assembly:AssemblyVersion("1.0.0.1")] [assembly:AssemblyCompany("Bawden Quinn Associates Limited")] [assembly:AssemblyDescription("A simple web service to create Umbraco categories / pages programmatically")] [WebService(Namespace="http://www.bawden-quinn.co.uk/webservices/umbraco",Description="Add a category / page programmatically")] public class umbSimpleWS { [WebMethod(Description = "Adds an Umbraco page")] public int addUmbPage(int startId, string categoryName, string pageTitle, string pageContent) { int catID = 0; int postID = 0; // Enter the document type alias that you use for the Category and Page (post) respectively string categoryDocTypeAlias = "Weblog Category"; string postDocTypeAlias = "Weblog Post"; // The attribute that holds the "content" on the page string pageContentHolder = "bodyText"; umbraco.BusinessLogic.User umbUser = new umbraco.BusinessLogic.User(0); // We need to identify if we have the category already in place beneath the parent (startID) XPathNodeIterator umbracoIterator = umbraco.library.GetXmlNodeById(startId.ToString()); XPathNavigator umbracoNodesNavigator = umbracoIterator.Current; XPathNodeIterator umbracoNodeElements = umbracoNodesNavigator.SelectDescendants(XPathNodeType.Element, false); while (umbracoNodeElements.MoveNext()) { if (umbracoNodeElements.Current.LocalName == "node") { if (umbracoNodeElements.Current.GetAttribute("nodeName", "") == categoryName) { // Found the category already exists catID = Convert.ToInt32(umbracoNodeElements.Current.GetAttribute("id", "")); } } } // Must create Category FIRST if not found if (catID == 0) { DocumentType categoryDocType = DocumentType.GetByAlias(categoryDocTypeAlias); Document objCategory = Document.MakeNew(categoryName, categoryDocType, umbUser, startId); objCategory.Publish(umbUser); umbraco.library.PublishSingleNode(objCategory.Id); umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Publish, new umbraco.BusinessLogic.User(0), objCategory.Id, "Added new Category '" + categoryName + "' programmatically"); // Important: the parent of the document will be the ID of the category just created catID = objCategory.Id; } // Now create the docment DocumentType postDocType = DocumentType.GetByAlias(postDocTypeAlias); Document objPost = Document.MakeNew(pageTitle, postDocType, umbUser, catID); objPost.getProperty(pageContentHolder).Value = pageContent; objPost.Publish(umbUser); umbraco.library.PublishSingleNode(objPost.Id); umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Publish, new umbraco.BusinessLogic.User(0), objPost.Id, "Added new Page '" + pageTitle + "' programmatically"); postID = objPost.Id; return (postID); } }