Sharepoint tips – advance

, , Leave a comment

Few tips related to sharepoint 2010

How to get list of Sharepoint 2010 Administration Web Apps programmatically without knowing the url ?

SPWebService.AdministrationService.WebApplications
Usage :
SPWebService.AdministrationService.WebApplications[guid/name]
SPWebService.AdministrationService.WebApplications.First()

Want to extend sharepoint web application programmatically ??

    /// <summary>
    /// This method helps you execute powershell command and
    /// returns you the result of the execution   
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    public static Collection<PSObject> ExecutePowerShellCommand(string command)
    {
        /*
         *  Configure Sharepoint Powershell snap ins !
         *  Needed to execute sharepoint related
         *  powershell commands
         */
        using (Runspace runspace = RunspaceFactory.CreateRunspace())
        {
          runspace.Open();
          PowerShell ps = PowerShell.Create();
          ps.AddScript("Add-PsSnapin Microsoft.SharePoint.PowerShell");
          ps.AddScript(command);
          ps.AddCommand("Out-String");
          Collection results = ps.Invoke();
          runspace.Close();
          return results;
        }
    }

   /// <summary>
   /// Extends the web application as per the specified parameters
   /// </summary>
   /// <param name="webApplicationUrl"></param>
   /// <param name="extendAppName"></param>
   /// <param name="hostHeader"></param>
   /// <param name="zone"></param>
   /// <param name="url"></param>
   /// <param name="port"></param>
   /// <returns></returns>
    public static Collection<PSObject> ExtendWebApplication(string webApplicationUrl, string extendAppName, string hostHeader, string zone, string url, int port)
    {
      StringBuilder pwShellScript = new StringBuilder();
      pwShellScript.Append(" Get-SPWebApplication -Identity ");
      pwShellScript.Append(webApplicationUrl + " | ");
      pwShellScript.Append(" New-SPWebApplicationExtension -Name " + extendAppName);
      if (!string.IsNullOrEmpty(hostHeader))
      {
        pwShellScript.Append(" -HostHeader " + hostHeader);
      }
      pwShellScript.Append(" -Zone " + zone);
      pwShellScript.Append(" -URL " + url);
      pwShellScript.Append(" -Port " + port);
      return ExecutePowerShellCommand(pwShellScript.ToString());
    }
Usage:
ExtendWebApplication(appUrl, .... various parameters);

Want to know available ports in IIS which can be used to create web app ? This may not be best way but then I couldn’t find any other way !!

    /// <summary>
    /// Generates Random number 
    /// </summary>
    /// <returns></returns>
    private static int GetRandomNumber()
    {
      Random rndPort = new Random();
      return rndPort.Next(8000, 15000);
    }
    /// <summary>
    /// Method validates if the specified portNumber is in use in IIS or is
    /// available to be used
    /// </summary>
    /// <param name="portNumber"></param>
    /// <returns></returns>
    public static bool IsUniquePort(int portNumber)
    {
      bool isUnique = true;
      ServerManager iisManager = new ServerManager();
      foreach (var site in iisManager.Sites)
      {
        if (site.Bindings.Count >0 && site.Bindings[0].EndPoint.Port.Equals(portNumber))
        {
          return false;
        }
      }
      return isUnique;
    }
    /// <summary>
    /// Method will return available free port number which can be used
    /// to create web apps
    /// </summary>
    /// <returns></returns>
    public static int GetUniquePortNumber()
    {
      bool uniquePortFound = false;
      int uniquePort = -1;
      do
      {
        uniquePort = GetRandomNumber();
        uniquePortFound = IsUniquePort(uniquePort);
      }
      while (!uniquePortFound);
      return uniquePort;
    }

usage :
int freePort = GetUniquePortNumber();