2

I followed the steps given in the below article:

http://www.codeproject.com/Tips/817340/Creating-Custom-Master-Page-for-Hosted-Apps

Created a Custom Master Page. Applied it to my App. Its working fine. But when I goto the master page gallery , I dont find the Master Page which I deployed through Visual Studio by adding a Module.

Below is the code in elements.xml file:

 <File Path="MasterPages\Test.master" Url="_catalogs/masterpage/Test.master" Type="GhostableInLibrary" IgnoreIfAlreadyExists="False"  ReplaceContent="True" />

I am not sure why I am unable to find this Master Page in the _catalogs/masterpage/Forms/AllItems.aspx

Please suggest.

Thank You in advance.

2 Answers 2

0

Try with following module code:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="MasterPage" Url="_catalogs/masterpage">
      <File Path="MasterPage\Test.master" Url="Test.master" Type="GhostableInLibrary" Level="Published"/>
  </Module>
</Elements>

I have just added Url property to whole module not a single master page file.

Hope this helps!

0

What you did was create a masterpage in the appweb. If you want it in the host web, you'll have to copy the masterpage to the host web's gallery. If you have a provider hosted app, this code would work to do so:

using (var clientContext = TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken, Request.Url.Authority))
{
    var site = clientContext.Site;

    //Upload master page from app web. Masterpage will be in the form of text since we only care about the markup and the masterpage should be already tested and fully functional within sharepoint

    //If masterpage was left as a masterpage file then there will be compile errors due to missing dll and references

    var streamMaster = this.OpenFile("/MasterPage/seattleCustom.txt");

    //Get context to the master page gallery.  Make sure the target site has Publishing features activated
    List gallery = site.RootWeb.Lists.GetByTitle("Master Page Gallery");

    //Create File Creation Information Object
    FileCreationInformation fcimaster = new FileCreationInformation();
    fcimaster.ContentStream = streamMaster;
    fcimaster.Url = "MPSHome.master";
    fcimaster.Overwrite = true;

    //Add File to Master Page Gallery
    Microsoft.SharePoint.Client.File newFile = gallery.RootFolder.Files.Add(fcimaster);
    clientContext.Load(newFile);
    clientContext.ExecuteQuery();

    //Get Masterpage from Gallery to set content type and metadata using caml query
    CamlQuery camlQuery = new CamlQuery();
    camlQuery.ViewXml = "<View><Query><Where><Contains><FieldRef Name='FileLeafRef'/><Value Type='Text'>" + fcimaster.Url + "</Value>" + "</Contains></Where></Query><RowLimit>100</RowLimit></View>";

    Microsoft.SharePoint.Client.ListItemCollection collListItem = gallery.GetItems(camlQuery);

    clientContext.Load(collListItem);
    clientContext.ExecuteQuery();

    //If not checked out, Check out File
    foreach (Microsoft.SharePoint.Client.ListItem item in collListItem)
    {
        clientContext.Load(item.File);
        clientContext.ExecuteQuery();
        if (item.File.CheckOutType == CheckOutType.None)
        {
            item.File.CheckOut();
        }

        //Get the ASP NET Master Page Content Type using helper function
        var masterPageCT = GetContentType(clientContext, gallery, "ASP NET Master Page");

        //Set Content Type, UI version, and Description. Check in and Publish
        item["ContentTypeId"] = masterPageCT.Id.ToString();
        item["UIVersion"] = Convert.ToString(15);
        item["MasterPageDescription"] = "This is the custom MPS MasterPage";
        item.Update();
        item.File.CheckIn("Updated Content Type", CheckinType.MajorCheckIn);
        item.File.Publish("Updated Metadata");
        clientContext.Load(item);
        clientContext.ExecuteQuery();
    }

}

Function to get content type

private ContentType GetContentType(ClientContext ctx, List docs, string contentType)
{
    ContentTypeCollection listContentTypes = docs.ContentTypes;
    ctx.Load(listContentTypes, types => types.Include
            (type => type.Id, type => type.Name, type => type.Parent));
    var result = ctx.LoadQuery(listContentTypes.Where(c => c.Name == contentType));
    ctx.ExecuteQuery();
    ContentType targetDocumentSetContentType = result.FirstOrDefault();
    return targetDocumentSetContentType;
}

I got the code from here. If you have a SharePoint-hosted app, then this will need to be converted to JSOM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.