Skip to main content

Create Document Dynamically (Umbraco)

You can create document and publish the content during run time, Below code snippets helps you to do it.
   public class UmbracoUtils  
   {  
  /// <summary>  
     /// Create and publish the document programatically  
     /// </summary>  
     /// <param name="nodeName"></param>  
     /// <param name="properties"></param>  
     /// <param name="documentType"></param>  
     /// <param name="parentId"></param>  
     /// <returns>node id</returns>  
     public static int CreateDocument(string nodeName, Dictionary<string, string> properties, int documentType, int parentId, List<string> roles = null)  
     {  
       DocumentType dt = new DocumentType(documentType);  
       umbraco.BusinessLogic.User u = new umbraco.BusinessLogic.User(0);  
       // Create the document  
       Document d = Document.MakeNew(nodeName, dt, u, parentId);  
       // Add values to the generic properties of the document   
       if (properties != null)  
       {  
         foreach (string property in properties.Keys)  
         {  
           d.getProperty(properties[property]).Value = properties[property];  
         }  
       }  
       // Set the publish status of the document and there by create a new version   
       if (roles != null)  
       {  
         int loginDocId = Constants.NODE_ID_HOME;  
         int errorDocId = Constants.NODE_ID_HOME;  
         umbraco.cms.businesslogic.web.Access.ProtectPage(false, d.Id, loginDocId, errorDocId);  
         foreach (string role in roles)  
         {  
           umbraco.cms.businesslogic.web.Access.AddMembershipRoleToDocument(d.Id, role);  
         }  
       }  
       d.Publish(u);  
       // Tell the runtime environment to publish this document   
       umbraco.library.UpdateDocumentCache(d.Id);  
       return d.Id;  
     }  
     }  
You could achieve this by calling directly as below,
  List<string> roles = new List<string>();  
       roles.Add(Constants.ROLE_COLLEGE);  
       roles.Add(Constants.ROLE_STUDENT);  
       UmbracoUtils.CreateDocument("MBA College", null, Constants.TEMPLATE_ID_CONTENTPAGE, Constants.NODE_ID_HOME, roles);  

Comments

  1. Hi, you didn't mention the namespaces to be used to get the above code work.

    ReplyDelete

Post a Comment

Popular posts from this blog

Beyond Solo Assistants: Google's Vision for AI Teamwork (Agent-to-Agent Collaboration)

We talk a lot about AI assistants like Google Assistant or chatbots answering our questions. They're pretty smart on their own, right? But imagine if they could team up, combine their unique skills, and tackle really complex problems together, just like a human team does. That's the core idea behind a super exciting area Google and others in the AI world are exploring: Agent-to-Agent (A2A) communication and collaboration. Think of it less as a single product called "Agent2Agent" and more as the science and engineering of building AI teams. Ready to explore why this is such a big deal? Let's break it down! First Off: What Even is an AI Agent? Think of an AI agent as a specialized digital helper. It's a piece of software designed to: Perceive: Understand its environment (text, images, data, user requests). Reason: Figure out the best course of action based on its goals and knowledge. Act: Perform tasks (answer questions, writ...

DD4TFormRouteHandler (posting a form as tridion page url)

ASP.NET routing enables us to use URLs that are not physical files, In DD4T we have the default page route definition. In which all page request redirct to Page controller and process the page. DD4TFormRouteHandler is a custom route handler responsible for mapping incoming browser requests to particular MVC controller actions, this works along with  DD4TFormHelper  (that generate the route information for the form) Posting a form in DD4T is not complicated, you can create the mvc form as a normal controller and action, then post it via AJAX. But, when we need to do post the form as normal page, It would need a tweak as the controller/action is not a page existed in tridion. This can be achieved by implementing a custom Mvc RoutHandler and reroute the posted form to the encrypted action and controller. It works as below daigram. So, how to do this. to render out the form we have BeginDD4TForm html helper as below that generate the form with encrypted route values....

CRM to SendEmail

Send email functionality with CRM internal static Boolean SendEmail(string toAddress, Guid? fromPartyGuid, string subjectLine, string body, Guid? contactId,string templateName) { CrmService crmService = GetCrmService(); //[[FROM activity party for the email. activityparty fromParty = new activityparty(); fromParty.partyid = new Lookup(); fromParty.partyid.type = EntityName.queue.ToString(); fromParty.partyid.Value = fromPartyGuid; //]] //[[TO activity party for email activityparty toParty = new activityparty(); toParty.partyid = new Lookup(); toParty.partyid.type = EntityName.contact.ToString(); toParty.partyid.Value = (contactId.HasValue) ? contactId.Value : new Guid(""); //]] //[[Create a new email and set its properties email emailInstance = new email(); //set email parameters emailInstance.from = new ac...