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);
Hi, you didn't mention the namespaces to be used to get the above code work.
ReplyDelete