Tuesday, 10 September 2013

Applying customer Master Page if it exists

Apply a custom master page to SharePoint web after checking if it exists in the master page gallery.

string masterPage = "_catalogs/masterpage/CustomMasterPage.master";
        string defaultMasterPage = "_catalogs/masterpage/seattle.master";
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                using (SPWeb currentWeb = properties.Feature.Parent as SPWeb)
                {
                    var file = currentWeb.Site.RootWeb.GetFile(masterPage);
                    if (file.Exists)

                    {
                        currentWeb.AllowUnsafeUpdates = true;
                        currentWeb.MasterUrl = currentWeb.Site.RootWeb.ServerRelativeUrl + "/" + masterPage;
                        currentWeb.CustomMasterUrl = currentWeb.Site.RootWeb.ServerRelativeUrl + "/" + masterPage;
                        currentWeb.Update();
                        currentWeb.AllowUnsafeUpdates = false;
                    }
                }
            }
            catch (Exception ex)
            {
               
            }
        }


Setting the master page to default in Feature Deactivation.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                using (SPWeb currentWeb = properties.Feature.Parent as SPWeb)
                {
                        currentWeb.AllowUnsafeUpdates = true;
                        currentWeb.MasterUrl = currentWeb.Site.RootWeb.ServerRelativeUrl + "/" + defaultMasterPage;
                        currentWeb.CustomMasterUrl = currentWeb.Site.RootWeb.ServerRelativeUrl + "/" + defaultMasterPage;
                        currentWeb.Update();
                        currentWeb.AllowUnsafeUpdates = false;
                }
            }
            catch (Exception ex)
            {
              
            }
        }
 

Tuesday, 3 September 2013

Log error in separate ULS Log file by restarting SharePoint Tracing Service.

Even though so many tools are available to view the ULS logs in SharePoint, it becomes very difficult and cumbersome to check for errors and share the actual errors with others if the ULS log is huge.

To overcome this , we can log only the events related to error in a separate log file by using SharePoint Tracing Service as shown below.

  1. Go to Run , Type Services.msc and press Enter.
  2. Find "SharePoint Tracing Service" and right click and select Restart.
  3. Now navigate to the folder where ULS logs are stored and sort by Modified Date, you can see that events will be logged in a separate log file.
  4. Execute the steps which is causing error in SharePoint environment and immediately restart the "SharePoint Tracing Service" again so that it starts writing the events into a new file.
  5. The second file from top will contain only those events related to the error we are looking for and it will be much smaller in size compared to other files.
  6. The small log file can be used to trace the error even if you don't have ULS viewer or LogViewer tool and can be easily shared with others.