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

Localization in Umbraco, Item Page field localization

Umbraco is one of the most deployed Web Content Management Systems on the Microsoft stack. It's in the top five most popular server applications and among the ten most popular open source tools in general. You can directly download and start using it from http://www.umbraco.com If you have not used the application yet, its always worth trying it once. I am sure you will love it. Well, for the first time users, Umbraco provide step by step instruction for initial set up, once you are done you can use some sample template or create a blank website. To know more on umbraco, Please refer below links http://our.umbraco.org/wiki/how-tos/a-complete-newbie's-guide-to-umbraco http://umbraco.com/help-and-support/video-tutorials/getting-started?freeVideos=1 http://our.umbraco.org/wiki/how-tos/getting-started-with-umbraco-what-is-next-after-you-install http://our.umbraco.org/wiki/install-and-setup ,  I have created a blank website, Now we will go step by step. Fi...

Why not to have a static const in c#

This is just a thought, that I was thinking why can't we have a constant with static in C#, and the answer is 'NO'; That we cannot have a static constant; e.g: I created a class as below: public class Constants1 { public const string Const1 = "Hello"; public const string Const2 = "World"; public static string Static1 = "Hello Static"; } When we compile the program into IL, the C# compiler does a magic in IL, that the constants converts into static literals, of course it has to, that's why we are able to access the constants as Constants1.Const1

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....