How to Publish or unpublish document from user control or custom module

harshal bundelkhandi asked on January 14, 2016 11:28

Hello Support,

I have created one custom module and also working on custom webparts. i just want to publish or unpublish the documents from my custom module.

Can you please suggest me in this.

Regards, Harshal

Recent Answers


Brenden Kehren answered on January 14, 2016 14:16

Your custom module should then include a custom page type if you want to use the OOTB publish and unpublish functionality. If you don't want to use that functionality, you'll have to include that functionality manually on the form for those classes by creating a custom form control to update the publish/unpublish dates.

0 votesVote for this answer Mark as a Correct answer

harshal bundelkhandi answered on January 16, 2016 12:12

Hi Brenden,

Can you please suggest me for OOTB publish and unpublish functionality. i don't have any idea about this.

also if i have to manually from which DB table this operation occurs or please suggest any API to do(if exist)

regards, Harshal

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on January 19, 2016 19:56

The API code to publish or unpublish is relatively simple, although if you have workflow / versioning you have to add some more code.

Once you have your custom Modules / Web part, and you access the "click" or whatever logic to get to it, here's a sample code segment to publish / unpublish. This can be found in your Kentico Menu -> Development -> API Examples -> Modules -> Content Management -> Workflow Advanced

Keep in mind the API Examples module in Kentico, SUPER helpful, didn't know it existed until about a month ago.

TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Prepare parameters
string siteName = SiteContext.CurrentSiteName;
string aliasPath = "/API-Example";
string culture = "en-us";
bool combineWithDefaultCulture = false;
string classNames = TreeProvider.ALL_CLASSNAMES;
string where = null;
string orderBy = null;
int maxRelativeLevel = -1;
bool selectOnlyPublished = false;
string columns = null;

// Get the document
TreeNode node = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree);

if (node != null)
{
    WorkflowManager workflowmanager = WorkflowManager.GetInstance(tree);

    // Make sure the document uses workflow
    WorkflowInfo workflow = workflowmanager.GetNodeWorkflow(node);

    if (workflow != null)
    {
        // Check if the workflow uses check-in/check-out
        bool autoCheck = !workflow.UseCheckInCheckOut(SiteContext.CurrentSiteName);

        // Create a new version manager instance
        VersionManager versionmanager = VersionManager.GetInstance(tree);

        // If it does not use check-in/check-out, check out the document automatically
        if (autoCheck)
        {
            versionmanager.CheckOut(node);
        }

        if (node.IsCheckedOut)
        {
            // Edit the last version of the document
            string newName = node.DocumentName.ToLower();

            node.DocumentName = newName;
            node.SetValue("MenuItemName", newName);

            // Save the document version
            DocumentHelper.UpdateDocument(node, tree);

            // Automatically check in
            if (autoCheck)
            {
                versionmanager.CheckIn(node, null, null);
            }

            return true;
        }
        else
        {
            apiEditDocument.ErrorMessage = "The page hasn't been checked out.";
        }
    }
    else
    {
        apiEditDocument.ErrorMessage = "The page doesn't use workflow.";
    }
}

return false;
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.