Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I use a feature to deploy a master page to a SharePoint 2010 publishing site. I use VS 2010 SharePoint projects. The feature deploys the .master , some CSS files and some images. Everything works OK except that all files deployed are in "Draft" state and I need to go and publish them manually.

What do I need to do so that when the feature is deployed, all the files are readily deployed as "published" ?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

I think this caused by one of the fantastic gotchas in SP2010. That is... if your solution is a Sandboxed solution the files aren't checked in/approved automatically - but farm solutions do check in/approve them.

If you're in a Sandboxed solution, you have to do this process manually - see http://blog.mastykarz.nl/automatically-publishing-files-provisioned-sandboxed-solutions/ for a good write up.

share|improve this answer

When files get deployed through features they should be in approved state already! There is something else going on here. Could you check the ULS logs etc. to see in there's not something missing? Is the feature perhaps Web scoped instead of Site scoped (which it should be for at least master pages)? The Elements tag should be set to RootWebOnly i.e.:

<Module Name="NAME" Url="_catalogs/masterpage" Path="LOCALPATHINFEATURE" RootWebOnly="TRUE">

And:

<Module Name="NAME" Url="Style Library/css" Path="LOCALPATHINFEATURE" RootWebOnly="TRUE">
share|improve this answer

When deploying master pages i use this feature event receiver to fix up the many possible things that can go wrong when deploying master pages via features.

public class LayoutsFeatureReceiver : Microsoft.SharePoint.SPFeatureReceiver {

    public override void FeatureActivated(SPFeatureReceiverProperties properties) {
        object parent = properties.Feature.Parent;
        if (parent is SPSite) {
            SPSite site = (SPSite)parent
            using (SPWeb web = site.RootWeb) {
                SPElementDefinitionCollection elementDefinitionCollection = properties.Definition.GetElementDefinitions(new CultureInfo(1033));
                foreach (SPElementDefinition elementDefinition in elementDefinitionCollection) {
                    XmlNode elementrootnode = elementDefinition.XmlDefinition;
                    _FeatureActivated(elementrootnode, properties.Feature.Definition.RootDirectory, web);
                }
            }
        }
    }

    public static void _FeatureActivated(XmlNode elementrootnode, string rootdirectory, SPWeb web) {
        XmlNamespaceManager ns = new XmlNamespaceManager(elementrootnode.OwnerDocument.NameTable);
        ns.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/");
        XmlNodeList nodes2 = elementrootnode.SelectNodes(".//sp:Property[@Name='FixUpFile']", ns);
        foreach (XmlNode node2 in nodes2) {
            bool yesno = node2.Attributes["Value"].Value == "TRUE";
            string name = node2.ParentNode.Attributes["Url"].Value;
            string url = node2.ParentNode.ParentNode.Attributes["Url"].Value + "/" + name;
            if (yesno) {
                SPFile file = web.GetFile(url);
                if (file.Exists) {
                    if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None) {
                        try {
                            file.UndoCheckOut();
                        } catch (SPException ex) {
                            if (ex.ToString().IndexOf("You cannot discard check out because there is no checked in version of the document. Please delete this document instead.") != -1) {
                                file.Delete();
                            } else {
                                throw ex;
                            }
                        }
                    }
                    if (file.CustomizedPageStatus == SPCustomizedPageStatus.Customized) {
                        file.RevertContentStream();
                    }
                    if (file.CustomizedPageStatus == SPCustomizedPageStatus.None) {
                        string rawdata = Path.Combine(rootdirectory, url);
                        byte[] data = File.ReadAllBytes(rawdata);
                        if (data.Length == 0) {
                            data = new byte[1];
                        }
                        file.CheckOut();
                        file.SaveBinary(data);
                        file.Update();
                        file.CheckIn("LayoutsFeatureReceiver:SaveBinary");
                        if (file.Item.ModerationInformation != null) {
                            file.Approve("LayoutsFeatureReceiver:SaveBinary");
                        }
                    }
                    file = web.GetFile(url);
                    if (file.CustomizedPageStatus != SPCustomizedPageStatus.Uncustomized) {
                        //err: failed to re-ghost page
                    }
                } else {
                    //err: file not provision'ed, most likely a user has created a file but not check'ed it in
                }
            }
        }
    }
}

Add the FixUpFile property to your master page element definition to trigger the fix up

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Module Name="MasterPagesModule" 
            Url="_catalogs/masterpage"
            RootWebOnly="True" 
            Path="_catalogs\masterpage">
        <File Url="custom.master" 
                IgnoreIfAlreadyExists="TRUE" 
                Type="GhostableInLibrary">
            <Property Name="ContentType" Value="$Resources:cmscore,contenttype_masterpage_name;" />
            <Property Name="Title" Value="Custom master page." />
            <Property Name="FixUpFile" Value="TRUE" />
        </File> 
    </Module>
</Elements>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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