Last active
December 19, 2015 20:38
-
-
Save janhebnes/86480ebfc36436380e36 to your computer and use it in GitHub Desktop.
Sitecore Webservice for creating a package and downloading it from webservice level. Part of the CruiseControl NANT Build Tasks that 1508 used back in 2010 on the phmetropol.dk project that won Sitecore Site of the Year two years in a row in the Educational category.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace FemtenNulOtte.NAnt.WebService | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text; | |
using System.Web; | |
using System.Web.Services; | |
using System.Xml.Serialization; | |
using FemtenNulOtte.NAnt.SharedLib; | |
using Sitecore.Configuration; | |
using Sitecore.Data; | |
using Sitecore.Data.Items; | |
using Sitecore.Data.Serialization; | |
using Sitecore.Globalization; | |
using Sitecore.Install; | |
using Sitecore.Install.Framework; | |
using Sitecore.Install.Items; | |
using Sitecore.Install.Utils; | |
using Sitecore.Install.Zip; | |
using Sitecore.Publishing; | |
using Sitecore.SecurityModel; | |
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. | |
// [System.Web.Script.Services.ScriptService] | |
/// <summary> | |
/// Summary description for NantWebService | |
/// </summary> | |
[WebService(Namespace = "http://1508.dk/NantWebService")] | |
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] | |
[System.ComponentModel.ToolboxItem(false)] | |
public class NantWebService : System.Web.Services.WebService | |
{ | |
#region Methods | |
[WebMethod] | |
public Byte[] CreatePackageAndReciveIt(List<SitecoreSerializedItem> items, string packageName) | |
{ | |
string packageFileName = string.Format("SitecoreItems_{0}.zip", packageName); | |
using (new SecurityDisabler()) | |
{ | |
PackageProject packageProject = new PackageProject(); | |
packageProject.Metadata.Author = "Webservice"; | |
packageProject.Metadata.PackageName = string.Format("SitecoreItems_{0}", packageName); | |
packageProject.SaveProject = false; | |
SourceCollection<PackageEntry> collection = new SourceCollection<PackageEntry>(); | |
foreach (SitecoreSerializedItem item in items) | |
{ | |
Database database = Factory.GetDatabase(item.Database); | |
Item sitecoreItem; | |
if(item.Type == SitecoreSerializedItem.MethodType.Remove) | |
{ | |
sitecoreItem = database.GetItem(item.Path.Replace(".item", string.Empty).Substring(0, item.Path.LastIndexOf("\\")).Replace("\\", "/")); | |
} | |
else | |
{ | |
sitecoreItem = database.GetItem(item.Path.Replace(".item", string.Empty).Replace("\\", "/")); | |
} | |
ItemSource source = new ItemSource(){SkipVersions = true, Database = sitecoreItem.Database.Name, Root = sitecoreItem.ID.ToString()}; | |
collection.Add(source); | |
} | |
ExplicitItemSource.Builder builder = new ExplicitItemSource.Builder(new ExplicitItemSource()); | |
builder.Initialize(new SimpleProcessingContext()); | |
new EntrySorter(collection).Populate(new Uniq(builder)); | |
builder.Finish(); | |
packageProject.Sources.Add(collection); | |
PackageWriter writer = new PackageWriter(Path.Combine(Environment.GetEnvironmentVariable("Temp"), packageFileName)); | |
writer.Initialize(new SimpleProcessingContext()); | |
PackageGenerator.GeneratePackage(packageProject, writer); | |
FileStream file = new FileStream(Path.Combine(Environment.GetEnvironmentVariable("Temp"), packageFileName), FileMode.Open, FileAccess.Read); | |
int lenght = (int) file.Length; | |
Byte[] fileContent = new byte[lenght]; | |
file.Read(fileContent, 0, lenght); | |
file.Close(); | |
return fileContent; | |
} | |
} | |
[WebMethod] | |
public string HelloWorld() | |
{ | |
return "Hello World"; | |
} | |
/// <summary> | |
/// Reverts the database by the sitecore path. | |
/// </summary> | |
/// <param name="items"> | |
/// The items. | |
/// </param> | |
/// <param name="publishItems"> | |
/// Should the items be published | |
/// </param> | |
/// <returns> | |
/// The revert sitecore build. | |
/// </returns> | |
[WebMethod] | |
public string RevertSitecoreBuild(List<SitecoreSerializedItem> items, bool publishItems) | |
{ | |
StringBuilder builder = new StringBuilder(); | |
using (new SecurityDisabler()) | |
{ | |
foreach (SitecoreSerializedItem item in items) | |
{ | |
if (item.Type == SitecoreSerializedItem.MethodType.Add || item.Type == SitecoreSerializedItem.MethodType.Change) | |
{ | |
Database database = Factory.GetDatabase(item.Database); | |
string fullPath = string.Format( | |
@"{0}\{1}{2}", | |
Settings.SerializationFolder, | |
item.Database, | |
item.Path).Replace("/", @"\"); | |
// verbose parameter | |
builder.AppendLine(fullPath); | |
// håndtere exceeption så der kommer en stor fed ERROR i loggen og at den kører videre på resten af items | |
Manager.LoadItem(fullPath, new LoadOptions() | |
{ | |
ForceUpdate = true, | |
DisableEvents = true, | |
Database = Factory.GetDatabase(item.Database) | |
}); | |
if (publishItems && item.Database == "master") | |
{ | |
PublishItem(item, database, false); | |
} | |
} | |
else if (item.Type == SitecoreSerializedItem.MethodType.Remove) | |
{ | |
string fullPath = string.Format( | |
@"{0}\{1}{2}", | |
Settings.SerializationFolder, | |
item.Database, | |
item.Path).Replace("/", @"\"); | |
fullPath = fullPath.Substring(0, fullPath.LastIndexOf("\\")); | |
Database database = Factory.GetDatabase(item.Database); | |
// verbose parameter | |
builder.AppendLine(fullPath); | |
// håndtere exception så der kommer en stor fed ERROR i loggen og at den kører videre på resten af items | |
Manager.LoadTree(fullPath, | |
new LoadOptions | |
{ | |
ForceUpdate = true, | |
DisableEvents = true, | |
Database = database | |
}); | |
if (publishItems && item.Database == "master") | |
{ | |
PublishItem(item, database, true); | |
} | |
} | |
} | |
} | |
return builder.ToString(); | |
} | |
private void PublishItem(SitecoreSerializedItem item, Database database, bool recursive) | |
{ | |
Item sitecoreItem; | |
if (item.Type == SitecoreSerializedItem.MethodType.Remove) | |
{ | |
string path = item.Path.Replace(".item", string.Empty).Substring(0, item.Path.LastIndexOf("\\")); | |
sitecoreItem = database.GetItem(path.Replace("\\", "/")); | |
} | |
else | |
{ | |
sitecoreItem = database.GetItem(item.Path.Replace(".item", string.Empty).Replace("\\", "/")); | |
} | |
foreach (Language language in database.Languages) | |
{ | |
PublishOptions options = new PublishOptions( | |
database, | |
Factory.GetDatabase("web"), | |
PublishMode.SingleItem, | |
language, | |
DateTime.Now) | |
{ | |
CompareRevisions = false, | |
RootItem = sitecoreItem, | |
Deep = recursive | |
}; | |
Publisher publisher = new Publisher(options); | |
publisher.Publish(); | |
} | |
} | |
#endregion Methods | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment