Wednesday, June 1, 2011

Start "Sharepoint 2010 Administration" Service using Powershell Script

Many a time we are into a situation that we have to deploy the WSP file by using Powershell script and it is required that if you want to deploy a wsp file in Sharepoint than "Sharepoint 2010 Administration" service has to be in Started status.

So if you want to start the "Sharepoint 2010 Administration" Service also through Powershell script than below is the snippet of Powershell script by using which we can start it before deploying the WSP.

Create a text file in notepad and add the below mentioned Script line and save this file as StartAdminService.ps1.

$AdminServiceName = "SPAdminV4"
$snapin="Microsoft.SharePoint.PowerShell"

if (get-pssnapin $snapin -ea "silentlycontinue")
{
    write-host -f Green "PSsnapin $snapin is loaded"
}
if ($(Get-Service $AdminServiceName).Status -eq "Stopped")
{
    $IsAdminServiceWasRunning = $false;
    Start-Service $AdminServiceName
    write-host -f Green "SPAdminV4 Service has been started Successfully."

}
else
{
    write-host -f Green "SPAdminV4 Service is already started."
}

Now drag and drop the text file in the Sharepoint 2010 Management Shell and Press Enter key. It will start the "Sharepoint 2010 Administration" service if it is not started.

Hope it will be a help to you.

Cheers!
Ravish

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;
             }
         }

Programmatically check a WSP is deployed on a Web Application or not

Sometimes we have a scenario like we have to check that whether a particular wsp (Solution Package) is deployed on a Web application or not.
Suppose we have a feature and on the feature activation we need to add a webpart on some page of a site but that webpart is not packaged in the Feature Deployment Solution Package rather it’s a part of some other wsp package.
So during the activation process we have to know whether the webpart which we are going to add through feature is deployed on the webapplication or not.
Below is the code snippet with the help of which we can check that whether a particular wsp package is deployed on a webapplication or not.
private static bool CheckWSPDeployedStatus(SPSite objSite)
        {
            System.Collections.ObjectModel.Collection<SPWebApplication> objWebApplications = objSite.WebApplication.Farm.Solutions["Sample.wsp"].DeployedWebApplications;
            if (objWebApplications != null && objWebApplications.Count > 0)
            {
                if (objWebApplications.Contains(objSite.WebApplication))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }