Publishing a document
To push content changes into the website xml cache, documents need to be published after the changes have been made. This is a 2 step process as the layer handling the database transactions (umbraco.cms.businesslogic.web.document) is unaware of the layer handling the runtime and the website xml cache (umbraco.presentation.nodefactory)
Therefore you first have to fetch the document you need, mark it ready for publishing, and then afterwards tell the runtime to update the cache for that specific item.
Using:
using umbraco.cms.businesslogic.web;
Publishing:
//Get the document by it's ID
Document d = new Document(1234);
//Mark it ready for publishing
d.Publish(new umbraco.BusinessLogic.User(0));
//Tell the runtime to update the document cache
umbraco.library.UpdateDocumentCache(d.Id);
Republishing an entire site
Publishing a document and republishing a site are 2 very different things:
"Publish" takes content from the database tables and creates an xml version of it, which is then pushed to the xml cache and saved in a table in the database for easy future access.
"Republish" only fetches pre-saved xml from the database, so this is a much quicker operation, but does not include any content not already converted to xml. Republish is used to ensure the xml cache is in sync with the published data in the database. This is also used to restore the umbraco.config file in case it's not being updated during publishing.
//call republishing from the api:
umbraco.library.RefreshContent();
Advanced republishing
If the umbraco.library.RefreshContent() call is not enough to get your site cache in order, due to either past errors or the xml not being generated properly, there are several ways to force parts of a website xml to be regerated from scratch.
Refresh the node XML
Server.ScriptTimeout = 100000;
umbraco.cms.businesslogic.web.Document.RePublishAll();
umbraco.library.RefreshContent();
Note: This can also be achieved by calling http;//YOURDOMAIN/Umbraco/dialogs/republish.aspx?xml=true and clicking "republish".
Refresh the preview XML
Server.ScriptTimeout = 100000;
umbraco.cms.businesslogic.web.Document.RegeneratePreviews();
umbraco.library.RefreshContent();
Note: This can also be achieved by calling http;//YOURDOMAIN/Umbraco/dialogs/republish.aspx?previews=true and clicking "republish".
Refresh a specific set of nodes' XML
Server.ScriptTimeout = 100000;
System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
var rootID = 1234;
//store children array here because iterating over an Array object is very inneficient.
var doc = new cms.businesslogic.web.Document(rootID);
var c = doc.Children;
foreach (cms.businesslogic.web.Document d in c)
{
d.XmlGenerate(xd);
}
umbraco.library.RefreshContent();
Note: This can also be achieved by calling http;//YOURDOMAIN/Umbraco/dialogs/republish.aspx?refreshNodes=1234 (where 1234 is the root node ID) and clicking "republish".