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;