29 December 2005

Adding an item and publishing in S5

using System;
using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Publishing;
using Sitecore.Configuration;
using Sitecore.SecurityModel;

namespace Addition.Test
{
///
/// Class to demo creation of an item and publishing.
///

public class S5Demos
{
// alternativelly paths can be used as well
public const string _masterId = "{E13FA2BC-FBB9-4F36-9601-C620E0FC6ED8}";
public const string _templateId = "{797D0FE3-16C6-4910-8174-68A226AACE67}";
public const string _insertionRoot = "{7776B388-75CE-4E06-A2A6-F7F6E053FFF8}";

// get an item from master database
public static Item GetItemFromSitecoreMaster(string itemPath)
{
return S5Demos.GetItemFromSitecore(itemPath, "master");
}

// get an item from web database
public static Item GetItemFromSitecoreWeb(string itemPath)
{
return S5Demos.GetItemFromSitecore(itemPath, "web");
}

// get an item based on GUID or path from a specified database
public static Item GetItemFromSitecore(string itemPath, string dbName)
{
Item itmResult = null;

Sitecore.Data.Database dataDB = new Sitecore.Data.Database(dbName);
if (dataDB != null)
{
Sitecore.Data.ItemUriBuilder bUri = new Sitecore.Data.ItemUriBuilder(itemPath, dataDB);
if (bUri != null)
{
Sitecore.Data.ItemUri uri = new Sitecore.Data.ItemUri(bUri.ItemUri);
if (uri != null)
{
itmResult = Sitecore.Data.Database.GetItem(uri);
}
}
}

return itmResult;
}

// create a sitecore node
protected void CreateAnItem(string itmName)
{
// where we insert?
Item root = GetItemFromSitecoreMaster(_insertionRoot);
// we can use a master as model
MasterItem masterItm = GetItemFromSitecoreMaster(_masterId);
// a tempalte is fine as well
TemplateItem templateItm = GetItemFromSitecoreMaster(_templateId);

using(SecurityDisabler disabler1 = new SecurityDisabler())
{
root.Editing.BeginEdit();

Sitecore.Diagnostics.Log.Info("adding " + itmName.ToString(), this);
Item itmAdd = root.Add(itmName, masterItm);
// or, by using the template: Item itmAdd = root.Add(itmName, templateItm);
if (itmAdd != null)
{
// modify a field
Sitecore.Data.Fields.Field fldLoc;

itmAdd.Editing.BeginEdit();
try
{
fldLoc = itmAdd.Fields["SomeField"];
if (fldLoc != null)
{
fldLoc.Value = "Some Value";
}
}
catch
{
}
finally
{
itmAdd.Editing.EndEdit();
}
}

root.Editing.EndEdit();
Sitecore.Diagnostics.Log.Info("adding done", this);
}

}


// publish a sitecore node
protected void PublishNode(Item itm)
{
Sitecore.Diagnostics.Log.Info("publishing node " + itm.Paths.Path, this);
try
{
PublishOptions aOptions = new PublishOptions(PublishMode.Full,
Sitecore.Globalization.Language.DefaultLanguage, DateTime.Now);
aOptions.TargetDatabase = Factory.GetDatabase("web");
aOptions.RootItem = itm;
aOptions.SourceDatabase = itm.Database;

//Create and run a publisher
Publisher aPublisher = new Publisher(aOptions);

// use a disabler to not be forced to log in
using (Sitecore.SecurityModel.SecurityDisabler disabler1 = new Sitecore.SecurityModel.SecurityDisabler())
{
aPublisher.Publish();
}
Sitecore.Diagnostics.Log.Info("publishing done for " + itm.Paths.Path, this);
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("error on publishing node " + itm.Paths.Path, ex, this);
}
}
}
}

1 comment:

Bryan said...

Thanks, helped me a lot!