Pages

Search This Blog

Showing posts with label Feature. Show all posts
Showing posts with label Feature. Show all posts

Monday, March 2, 2015

Features Installed but not showing in SharePoint

Got a very strange problem with the features. Even though the solution is installed and features are showing in the features folder but the same features are not appearing in the SharePoint UI
Run SharePoint 2010 / 2013 Management Shell from the central admin server and 
run following command
Install-SPFeature -ScanForFeatures 
This will show you any features that are available in the SharePoint Root but have not been installed. 
You can install any missing features using the command :-
Install-SPFeature -AllExistingFeatures 
See the following TechNet Article for more information.

Tuesday, February 21, 2012

Programatically set default Page layout of site to Custom page layout

In my previous post, we have learned how to create custom page layouts using feature.

Requirement:
We need to set the custom page layout created as the default page layout of the site.
Say if a user creates a new page, it should create a page which is using the custom layout.
We need to achieve this programatically.

Solution:

We can achieve this by writing the code within the receiver of the feature created.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{   
    SPSite site = properties.Feature.Parent as SPSite;
    SPWeb web = null;
    if (site == null)
    {
         web = properties.Feature.Parent as SPWeb;
         if (web == null) return;
         site = web.Site;
    }
    else web = site.RootWeb;

       //Get the publishing web object
    PublishingWeb objPublishingWeb = PublishingWeb.GetPublishingWeb(web);
    //Set default page layout of site to internal page layout  (InnerPage.aspx)             
    if (objPublishingWeb != null)
    {
       PageLayout _pageLayout = (from _pl in objPublishingWeb.GetAvailablePageLayouts()
                               where _pl.Name == "InnerPage.aspx"
                               select _pl).FirstOrDefault();
       objPublishingWeb.SetDefaultPageLayout(_pageLayout, true);
       objPublishingWeb.Update();
    }
}


Tuesday, January 10, 2012

Sharepoint Adding a Mapped URL programmatically

In Sharepoint 2010, you can retrieve the URL of some common Application Pages as AccessDenied.aspx, Error.aspx, Login.aspx, Signout.aspx etc. This has been done for the purpose of enabling the user to assign custom Application page instead of the default one. One way to do this is to add a Mapping attribute in the web.config file. But in Sharepoint 2010, there is a simpler way to do this.
We can make use of GetMappedPage and UpdateMappedPage  methods. This can be done by creating a Web scoped Feature and write your code in Feature Activated and Deactivated as:



public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
                try
                {
                    if (webApp != null)
                    {
                        if (!webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.AccessDenied,
                            "/_layouts/YourCustomAppPagesFolder/CustomAccessDenied.aspx"))
                        {
                            throw new ApplicationException("Error adding mapped page.");
                        }

                        // File Not Found page is set differently.
                        webApp.FileNotFoundPage = "/_layouts/1033/YourCustomFolder/YourFileNorFoundPage.htm";
                        webApp.Update(true);
                    }
                }
                catch (Exception Ex)
                {

                }
            }


            public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
                SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
                try
                {
                if (webApp != null)
                {
                    //you have to set the property back to null for setting it to default
                    webApp.UpdateMappedPage(SPWebApplication.SPCustomPage.AccessDenied, null);

                    //File not found is handled differently
                    webApp.FileNotFoundPage = null;
                }
                }
                catch (Exception Ex)
                {
                }
            }
        }

Sunday, April 24, 2011

Programmatically change the Welcome / Default page of a Sharepoint Publishing Web

Sometimes we have a requirement like on feature activation or deactivation we have to change the Welcome Page(Default Page) of the publishing site.
Below is the code snippet with the help of which we can change the Welcome Page:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPSite objSiteProperty = (SPSite)properties.Feature.Parent)
            {
                using (SPWeb objSPWeb = objSiteProperty.OpenWeb())
                {
                    PublishingWeb objPublishingWeb = PublishingWeb.GetPublishingWeb(objSPWeb);
                    SPFile objHomePageFile = objSPWeb.GetFile("Pages/Test.aspx");
                    objPublishingWeb.DefaultPage = objHomePageFile;
                    objPublishingWeb.Update();
                }
            }
        }

And in the same way on Feature Deactivation we can change the Welcome Page again.

Monday, April 18, 2011

Access denied while activating feature in Sharepoint 2010



In my recent project I encountered a problem with Feature activation. I had to deploy some web.config changes using the package and then needed to propagate those changes across all the WFE’s on the server.  So I thought of creating a “Web Application” scoped feature to propagate the changes. But I received “Access Denied” while activating the feature from SharePoint. However when I tried doing the same through command line, it worked. 

After doing some R&D on this, I discovered that in SharePoint 2010, a new security feature has been added to all objects inheriting from SPPersistedObject in the Microsoft.SharePoint.Administration namespace. This feature explicitly disallows modification of the above stated objects from content web applications. The error message thrown was also very misleading but after some more tracing through the code I found a property in SharePoint API which controls this behavior. The property is:

“Microsoft.SharePoint.Administration.SPWebService.ContentService.RemoteAdministratorAccessDenied”

You can write this to your custom feature before executing your custom code.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
              if (SPWebService.ContentService.RemoteAdministratorAccessDenied == true)
                {
                    SPWebService.ContentService.RemoteAdministratorAccessDenied = false;
                    SPWebService.ContentService.Update(true);
                }

              // Your custom logic goes here

            }
            catch (Exception Ex)
            {
// In case of any exception, you can easily trace it in the System Event //viewer
System.Diagnostics.EventLog.WriteEntry("Your custom Message", Ex.StackTrace, System.Diagnostics.EventLogEntryType.Error);
            }
        }


I have also written a PowerShell script for the same. Copy the below code and paste the contents in Note pad and save it with extension as .ps1

function LoadSharePointPowerShellEnvironment
{
   write-host
   write-host "Setting up PowerShell environment for SharePoint..." -foregroundcolor Yellow
   write-host
   Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
   write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
}

function SetRemoteAdministratorAccessDenied()
{
       # load sharepoint API libs
       [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
       [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Administration") > $null

  # First get the content web service
 $contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
  # Now set the remote administration security to off
 $contentService.RemoteAdministratorAccessDenied = $false
  # Also update the web service
 $contentService.Update()
        
}


#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                          Configuring RemoteAdministratorAccessDenied
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#

write-host
LoadSharePointPowerShellEnvironment

SetRemoteAdministratorAccessDenied




Monday, April 11, 2011

Programmatically check a Feature is activated on a SiteCollection or Not

Sometime we have a requirement like while deactivating a feature we need to check programmatically weather that Particular feature is deployed to other site collections of the same Web Application or not.
 Below is the code snippet with the help of which we can check it:
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
         {
             base.FeatureDeactivating(properties);            
             String URL;
             URL = ((SPSite)properties.Feature.Parent).Url;
             bool isFeatureActiveInOtherSiteCollection = false;
             
             try
             {
                 SPSecurity.RunWithElevatedPrivileges(delegate()
                         {
                             using (SPSite objSite = new SPSite(URL))
                             {
                                 using (SPWeb objWeb = objSite.OpenWeb())
                                 {
                                     SPWebApplication objSPWebApp = objSite.WebApplication;

                                     foreach (SPSite objOtherSite in objSPWebApp.Sites)
                                     {
                                         if (!objOtherSite.Url.Equals(objSite.Url) && objOtherSite.Features[properties.Feature.DefinitionId] != null)
                                         {
                                             isFeatureActiveInOtherSiteCollection = true;
                                             break;
                                         }
                                     }
                                 }
                             }
                         });
             }                    
             catch (Exception ex)
             {
                 throw ex;
             }
         }

Friday, February 11, 2011

Reading XML Through Feature

In a scenario we have to create the whole site on the activation of feature, so for that we used a xml, which contains all the information regarding it.
This posts shows how we can read the xml (custom.xml) through feature, the custom.xml should be placed along with the feature.xml

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
XmlNodList objRootNode;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(properties.Definition.RootDirectory + "XML_FILE_NAME");
objRootNode = xmlDoc.SelectNodes("ChannelStaffing");
foreach (XmlNode objNode in objRootNode[0].ChildNodes)
{
foreach(XmlNode objChildNode in objNode.ChildNodes)
{
//Your code here
}
}
}