Skip to content

Commit

Permalink
Merge pull request #874 from SubPointSolutions/dev
Browse files Browse the repository at this point in the history
SPMeta2 v1.2.80, August 2016
  • Loading branch information
SubPointSupport authored Aug 15, 2016
2 parents 6f3796a + 3943b37 commit a91db27
Show file tree
Hide file tree
Showing 84 changed files with 6,614 additions and 3,945 deletions.
2 changes: 1 addition & 1 deletion SPMeta2/SPMeta2.Build/m2.buildbaseline.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion SPMeta2/SPMeta2.CSOM.Standard/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.2.16220.0718")]
[assembly: AssemblyFileVersion("1.2.16227.1126")]
206 changes: 206 additions & 0 deletions SPMeta2/SPMeta2.CSOM/ModelHandlers/Base/MasterPageModelHandlerBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint.Client;
using SPMeta2.Common;
using SPMeta2.CSOM.Extensions;
using SPMeta2.CSOM.ModelHosts;
using SPMeta2.Definitions;
using SPMeta2.Enumerations;
using SPMeta2.Utils;

namespace SPMeta2.CSOM.ModelHandlers.Base
{
public abstract class MasterPageModelHandlerBase : CSOMModelHandlerBase
{
#region properties

public abstract string PageContentTypeId { get; set; }
public abstract string PageFileExtension { get; set; }

#endregion

#region methods
protected string GetSafePageFileName(PageDefinitionBase page)
{
var fileName = page.FileName;

if (!fileName.ToLower().EndsWith(PageFileExtension.ToLower()))
{
fileName += PageFileExtension;
}

return fileName;
}


protected File GetCurrentPage(List list, Folder folder, string pageName)
{
var item = SearchItemByName(list, folder, pageName);

if (item != null)
return item.File;

return null;
}

protected ListItem SearchItemByName(List list, Folder folder, string pageName)
{
var context = list.Context;

if (folder != null)
{
if (!folder.IsPropertyAvailable("ServerRelativeUrl"))
{
folder.Context.Load(folder, f => f.ServerRelativeUrl);
folder.Context.ExecuteQueryWithTrace();
}
}

var dQuery = new CamlQuery();

string QueryString = "<View><Query><Where>" +
"<Eq>" +
"<FieldRef Name=\"FileLeafRef\"/>" +
"<Value Type=\"Text\">" + pageName + "</Value>" +
"</Eq>" +
"</Where></Query></View>";

dQuery.ViewXml = QueryString;

if (folder != null)
dQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;

var collListItems = list.GetItems(dQuery);

context.Load(collListItems);
context.ExecuteQueryWithTrace();

return collListItems.FirstOrDefault();

}

protected File FindPageFile(List list, Folder folder, MasterPageDefinitionBase definition)
{
var pageName = GetSafePageFileName(definition);
return GetCurrentPage(list, folder, pageName);
}

protected ListItem FindPage(List list, Folder folder, MasterPageDefinitionBase definition)
{
var pageName = GetSafePageFileName(definition);

return FindPageByName(list, folder, pageName);
}

protected ListItem FindPageByName(List list, Folder folder, string pageName)
{
var file = GetCurrentPage(list, folder, pageName);

if (file != null)
return file.ListItemAllFields;

return null;
}

public override void DeployModel(object modelHost, DefinitionBase model)
{
var folderModelHost = modelHost.WithAssertAndCast<FolderModelHost>("modelHost", value => value.RequireNotNull());
var definition = model.WithAssertAndCast<MasterPageDefinitionBase>("model", value => value.RequireNotNull());

var folder = folderModelHost.CurrentListFolder;
var list = folderModelHost.CurrentList;

var context = folder.Context;

var pageName = GetSafePageFileName(definition);
var currentPageFile = GetCurrentPage(list, folder, pageName);

InvokeOnModelEvent(this, new ModelEventArgs
{
CurrentModelNode = null,
Model = null,
EventType = ModelEventType.OnProvisioning,
Object = currentPageFile,
ObjectType = typeof(File),
ObjectDefinition = definition,
ModelHost = modelHost
});

ModuleFileModelHandler.WithSafeFileOperation(list, currentPageFile, f =>
{
var file = new FileCreationInformation();

file.Url = pageName;
file.Content = definition.Content;
file.Overwrite = definition.NeedOverride;

return folder.Files.Add(file);

},
newFile =>
{
var newFileItem = newFile.ListItemAllFields;
context.Load(newFileItem);
context.ExecuteQueryWithTrace();

var site = folderModelHost.HostSite;
var currentPageLayoutItem = FindPageLayoutItem(site, definition.FileName);


var currentPageLayoutItemContext = currentPageLayoutItem.Context;
var publishingFile = currentPageLayoutItem.File;

currentPageLayoutItemContext.Load(currentPageLayoutItem);
currentPageLayoutItemContext.Load(currentPageLayoutItem, i => i.DisplayName);
currentPageLayoutItemContext.Load(publishingFile);

currentPageLayoutItemContext.ExecuteQueryWithTrace();

newFileItem[BuiltInInternalFieldNames.Title] = definition.Title;
newFileItem["MasterPageDescription"] = definition.Description;
newFileItem[BuiltInInternalFieldNames.ContentTypeId] = PageContentTypeId;

if (definition.UIVersion.Count > 0)
{
newFileItem["UIVersion"] = string.Join(";#", definition.UIVersion.ToArray());
}

newFileItem["DefaultCssFile"] = definition.DefaultCSSFile;

newFileItem.Update();

context.ExecuteQueryWithTrace();
});

currentPageFile = GetCurrentPage(folderModelHost.CurrentList, folder, pageName);

InvokeOnModelEvent(this, new ModelEventArgs
{
CurrentModelNode = null,
Model = null,
EventType = ModelEventType.OnProvisioned,
Object = currentPageFile,
ObjectType = typeof(File),
ObjectDefinition = definition,
ModelHost = modelHost
});

context.ExecuteQueryWithTrace();
}

private ListItem FindPageLayoutItem(Site site, string pageLayoutFileName)
{
var rootWeb = site.RootWeb;
var layoutsList = rootWeb.GetCatalog((int)ListTemplateType.MasterPageCatalog);

var layoutItem = SearchItemByName(layoutsList, layoutsList.RootFolder, pageLayoutFileName);

return layoutItem;
}

#endregion
}
}
38 changes: 38 additions & 0 deletions SPMeta2/SPMeta2.CSOM/ModelHandlers/HtmlMasterPageModelHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using SPMeta2.Common;
using SPMeta2.CSOM.Extensions;
using SPMeta2.CSOM.ModelHandlers.Base;
using SPMeta2.CSOM.ModelHosts;
using SPMeta2.Definitions;
using SPMeta2.Enumerations;
using SPMeta2.Utils;

namespace SPMeta2.CSOM.ModelHandlers
{
public class HtmlMasterPageModelHandler : MasterPageModelHandlerBase
{
#region properties

public override Type TargetType
{
get { return typeof(HtmlMasterPageDefinition); }
}

public override string PageContentTypeId
{
get { return BuiltInSiteContentTypeId.HtmlMasterPage; }
set { }
}

public override string PageFileExtension
{
get { return ".html"; }
set { }
}

#endregion
}
}
Loading

0 comments on commit a91db27

Please sign in to comment.