0

I have got masterpage and css related to it given by our designer. I want to create master page package programmatically. I have created feature for it and added event reciever to it. But i dont know exactly where to put masterpage and css files in project. Is there good way to keep this files? Should i create folder for mastepage and css files seperately. I hope this is correct way to apply masterpage .

private static void ApplyMasterPage(SPSite site, string url, string systemUrl)
    {
        try
        {
            using (SPWeb rootWeb = site.RootWeb)
            {
                Uri masterUri = new Uri(string.Format(url, rootWeb.Url));
                Uri systemUri = new Uri(string.Format(systemUrl, rootWeb.Url));
                rootWeb.CustomMasterUrl = masterUri.AbsolutePath;
                rootWeb.MasterUrl = systemUri.AbsolutePath;
                rootWeb.Update();


                foreach (SPWeb web in site.AllWebs)
                {

                    SPSecurity.RunWithElevatedPrivileges(delegate ()
                    {
                        if (!web.IsRootWeb)
                        {
                            web.CustomMasterUrl = masterUri.AbsolutePath;
                            web.MasterUrl = systemUri.AbsolutePath;
                            web.AllowUnsafeUpdates = true;
                            web.Update();
                        }
                    });
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

In masterpage css is referred as below

 <link id="Link3" rel="stylesheet" runat="server" type="text/css" href="<% $SPUrl:~SiteCollection/Style%20Library/mycss.css%>" media="screen,projection"/>

I wish to know below things

  1. Where to put masterpage and css files in solution?
  2. How to refer css files in code.
4

Typically I create a module to host master pages, deployed to _catalogs/masterpage, a mapped layouts for css and a mapped images for images.

If you want to store at the site level, you can use modules for that too.

e.g. mp module

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="MasterPages" Path="MasterPages" Url="_catalogs/masterpage" List="116">
    <File Path="Custom.master" Url="Custom.master" ReplaceContent="TRUE" Type="GhostableInLibrary" >
      <Property Name="UIVersion" Value="15"></Property>
      <Property Name="MasterPageDescription" Value="My Custom Master Page" />
      <Property Name="ContentType" Value="$Resources:cmscore,contenttype_masterpage_name;" />
    </File>    
  </Module>

e.g. style lib module

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="Styles" Url="Style Library">    
    <File Path="Styles\mycss.css" Url="mycss.css" Type="GhostableInLibrary"/>

  </Module>
</Elements>

example file structure:

>solution
 > project
   > masterpages (module)
     > custom.master
   > styles (module)
     > mycss.css
     > images
       > image1.png

More here:

Using Modules to Include Files in the Solution

  • but where i keep css and mp in visual studio sokution? – Happy Mar 21 '16 at 10:40
  • modules are like folders and you should keep css, images and master pages inside of them. then modules deploy these files to specific sites so you won't have to worry about manually provisioning these elements. they can be associated with features – Tiago Duarte Mar 21 '16 at 10:43
  • in my master page css referred as this <link id="Link3" rel="stylesheet" runat="server" type="text/css" href="<% $SPUrl:~SiteCollection/Style%20Library/mycss.css%>" media="screen,projection"/> so when i add module and css in it then do i need to change path of css in master page and if yes then what path should be there? – Happy Mar 22 '16 at 9:49
  • the path is the project relative folder for the file, while url is the website relative location. I have updated the style lib example to match your current css reference. the module is referencing the style library and the file is referencing the root, which is what your link url states. so as long as you have a module "styles" with a file "mycss.css" inside of that – Tiago Duarte Mar 22 '16 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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