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

Please don't mock my code, I am just wondering why this does not work for Style Library but it works for the Master Page Gallery

                if (siteCollection != null)
            {
                SPList masterPageLibrary = rootWeb.Lists["Style Library"];
                if (masterPageLibrary != null)
                {

                    string featureId = properties.Feature.Definition.Id.ToString();

                    if (String.IsNullOrEmpty(featureId))
                    {
                        throw new ArgumentNullException("featureId");
                    }
                    SPFileCollection files = masterPageLibrary.RootFolder.Files;
                    var checkedOutFiles = from SPFile f
                                          in files
                                          where String.Equals(f.Properties["FeatureId"] as string, featureId, StringComparison.InvariantCultureIgnoreCase)
                                          select f;


                    foreach (SPFile f in checkedOutFiles)
                    {
                            try
                            {
                                f.CheckOut();
                                f.Update();
                            }
                            catch { }
                            //try to check in
                            try
                            {
                                f.CheckIn("Automatically checked in by feature activation", SPCheckinType.MajorCheckIn);
                                f.Update();
                            }
                            catch { }
                            //try to publish
                            try
                            {
                                f.Item.File.Publish("automatically published by feature activation");
                                f.Update();
                            }
                            catch { }


                            //try to approve
                            try
                            {
                                f.Item.File.Approve("Automatically approved by feature activation");
                                f.Update();

                            }
                            catch { }
                        }

                }
            }

Thanks in advance

Matthew Hughes

share|improve this question
    
can you debug it? Where is it failing and is what is the error message(if any)? –  Derek Gusoff May 17 '12 at 20:46
2  
also, you should use rootWeb.Lists.TryGetList[listname] instead of rootWeb.Lists[listname] as the former gives you a chance to avoid throwing an exception if the list is not found –  Derek Gusoff May 17 '12 at 20:49

2 Answers 2

You're only looking for files in the RootFolder, which is where all the files are in the Master Page Gallery, but usually none of the files in Style Library

share|improve this answer

Probably you should attempt using a piece from the example available on stackoverflow (the title you should use is "Master Page Gallery" to be sure)

http://stackoverflow.com/questions/1716074/retrieve-all-master-pages-for-a-sharepoint-site

Looping through list items can be achieved in multiple ways:

  1. Starting from the example code would need to adapt the CAML query to target specific items to reduce the items returned (e.g the example searches for .master pages) and reuse the piece of code looping through list items while checking for the File property is is not NULL(means that you have a file) or for the Folder property (means that you are accessing a folder).

  2. If you know the exact path to your Folder start loading in there using something like (see here for more folder operations examples http://avinashkt.blogspot.com/2010/03/spfolder-related-operations-in.html):

    parentWeb.GetFolder(folderName); if (folder != null) { //start looping }

share|improve this answer
    
He is trying to get the code which works when using "Master Page Gallery" to work with "Style Library" –  Per Jakobsen May 18 '12 at 7:17

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.