Friday, September 21, 2012

Error Type: Microsoft SharePoint is not supported with version 4.0.30319.17626 of the Microsoft .Net Runtime (Powershell).


After installing SharePoint 2010 on Windows Server 2008 R2 am getting the following while executing powershell cmdlet i.e
 
"Microsoft SharePoint is not supported with version 4.0.30319.17626 of the Microsoft .Net Runtime"



Then i checked version of the powershell using cmdlet "$PSVersionTable"

 
 
In order to make use of the powershell cmdlets, need to switch powershell version back to version 2.
 
Note: This will not uninstall version 3, it will launch a new instance that use version 2.
 
Powershell cmdlet to switch powershell version 2 is "powershell -version 2"
 
 
Then run Add-PSSnapin powershell cmdlet
 
 
 
Now powershell switch to version 2, we can run powershell cmdlets ..
 
 
Thats it...



Thursday, September 20, 2012

Programmatically how to create site collections in SharePoint


Method 1

In this method first i will create new web application and then create first site collection in that web application.



            try
            {
                int port = Convert.ToInt32(txtPortNo.Text);
                string UserName = txtUN.Text;
                string DBName = txtDBName.Text;
                string ServerName = txtServerName.Text;
                string SiteCollectionTitle = txtSiteCollectionTitle.Text;
                string SiteCollectionDesc = txtSiteColDesc.Text;
                string SiteColOwner = txtSiteColOwner.Text;
                string SiteColOwnerEmail = txtEmailAddress.Text;
                bool isAvail = CheckPortUsage(port);
                if (isAvail)
                {
                    SPWebApplicationBuilder builder = new SPWebApplicationBuilder(SPFarm.Local);
                    SPWebApplication newApplication;
                    builder.Port = port;
                    builder.ApplicationPoolId = "SharePoint - " + port.ToString();
                    builder.IdentityType = IdentityType.SpecificUser;
                    SPFarmManagedAccountCollection manaccountcollection = new SPFarmManagedAccountCollection(SPFarm.Local);
                    SPManagedAccount maccount = manaccountcollection.FindOrCreateAccount(@UserName);
                    builder.ManagedAccount = maccount; //use the SPManagedAccount to receive the username and password automatically
                    builder.RootDirectory = new System.IO.DirectoryInfo("C:\\Inetpub\\wwwroot\\wss\\VirtualDirectories\\" + port.ToString());
                    builder.CreateNewDatabase = true;
                    builder.DatabaseName = DBName;
                    builder.DatabaseServer = ServerName;
                    builder.UseNTLMExclusively = true;
                    newApplication = builder.Create(); // Create new web application
                    newApplication.Name = "SharePoint-" + port;
                    newApplication.Provision();
                    SPSite mySiteCollection = newApplication.Sites.Add("/", SiteCollectionTitle, SiteCollectionDesc, 1033, "STS#0", @UserName, SiteColOwner, SiteColOwnerEmail);
                    mySiteCollection.Close();
                    //display message like "Web Application and site collection Created Successfully"
                }
                else
                {
                   //display message like "Port Not available"
                }

            }
            catch (Exception ex)
            {
                //show the exception
            }



 public static bool CheckPortUsage(int port)
        {
            try
            {
                new TcpClient(new IPEndPoint(IPAddress.Any, port)).Close();
                return true;
            }
            catch
            {
                return false;
            }
        }


Method 2:


Creating site collection under root web application i.e '/' if already root site collection created then it will through an exception
            SPSite site = null;
            try
            {
                SPWebApplication webApplication = null;
                webApplication = SPWebApplication.Lookup(new Uri("http://ukreddy:8998"));
                site = webApplication.Sites.Add("/", "firstsitecollection", "sitecolection desc", 1033, "STS#0", "UKREDDYSYKAM\\administrator", "UKREDDYSYKAM\\administrator", "s.urkeddy@gmail.com");

            }
            catch (Exception ex)
            {
                //Display error message.
            }
            finally
            {
                if (site != null)
                    site.Close();
            }


Method 3:

If the root site collection is already created then above method through an exception like " ".
Then need to create new site collection under managed path i.e /sites/


            SPSite site = null;
            try
            {
                SPWebApplication webApplication = null;
                webApplication = SPWebApplication.Lookup(new Uri("http://ukreddy:8998"));
                site = webApplication.Sites.Add("/sites/siteCollname", "sitecollectiontitle", "sitecolection desc", 1033, "STS#0", "UKREDDYSYKAM\\administrator", "UKREDDYSYKAM\\administrator", "s.urkeddy@gmail.com");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (site != null)
                    site.Close();
            }




Tuesday, September 18, 2012

Programmatically How to delete all web applications other than central administration




In previous post shown how to get all available web applications.
Here the code snippet for delete all web applications other than central administration

Method 1:


            SPWebApplicationCollection webapps = SPWebService.ContentService.WebApplications;
            foreach (SPWebApplication webapp in webapps)
            {
                foreach (SPAlternateUrl url in webapp.AlternateUrls)
                {
                        foreach (SPContentDatabase db in webapp.ContentDatabases)
                        {
                            db.Unprovision();
                        }
                        webapp.UnprovisionGlobally();
                        webapp.Delete();
                }
            }


Method 2:

If you dont want to delete some of required web application then specify those web application       names here otherwise comment if condition. Here i dont want to delete http://ukreddy:6969,http://ukreddy:80/ and http://ukreddy:2010 web application

            SPWebApplicationCollection webapps = SPWebService.ContentService.WebApplications;
            foreach (SPWebApplication webapp in webapps)
            {
                foreach (SPAlternateUrl url in webapp.AlternateUrls)
                {
                    if (url.Uri.ToString() != "http://ukreddy:6969/" && url.Uri.ToString() != "http://ukreddy/" && url.Uri.ToString() != "http://ukreddy:2010/")
                    {
                        foreach (SPContentDatabase db in webapp.ContentDatabases)
                        {
                            db.Unprovision();
                        }
                        webapp.UnprovisionGlobally();
                        webapp.Delete();
                    }
                }
            }

Programmatically How to get all web application details in SharePoint 2010



In previous post shown how to create new web application using server object model.

Now here the code snippet for get all web applications using server object model in SharePoint 2010

Method 1:


           string strGetAllWebApps = "";
            SPServiceCollection services = SPFarm.Local.Services;
            foreach (SPService service in services)
            {
                if (service is SPWebService)
                {
                    SPWebService webservice = (SPWebService)service;
                    foreach (SPWebApplication webapp in webservice.WebApplications)
                    {
                        foreach (SPAlternateUrl url in webapp.AlternateUrls)
                        {
                            strGetAllWebApps+= url.Uri + "\n";
                        }
                    }
                }
            }
            


Method 2:


            string strGetAllWebApps2 = "";
            SPWebApplicationCollection webapps = SPWebService.ContentService.WebApplications;
            foreach (SPWebApplication webapp in webapps)
            {
                foreach (SPAlternateUrl url in webapp.AlternateUrls)
                {
                        strGetAllWebApps2 += url.Uri + "\n"; 
                }
            }

Programmatically How to create web application in SharePoint 2010 using Server Object Model




Here the code snippet to create a new web application in SharePoint 2010 using Server Object Model.


Step 1:

Add the following namespace to project


using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData.SystemSpecific;

Step 2:

Write the following code for creating SPWebApplication

Here am taking some values from text boxes


            try
            {
                int port = Convert.ToInt32(txtPortNo.Text);
                string UserName = txtUN.Text;
                string DBName = txtDBName.Text;
                string ServerName = txtServerName.Text;
                string SiteCollectionTitle = txtSiteCollectionTitle.Text;
                string SiteCollectionDesc = txtSiteColDesc.Text;
                string SiteColOwner = txtSiteColOwner.Text;
                string SiteColOwnerEmail = txtEmailAddress.Text;
                bool isAvail = CheckPortUsage(port);//Checking port is in use or not
                if (isAvail)
                {
                    SPWebApplicationBuilder builder = new SPWebApplicationBuilder(SPFarm.Local);
                    SPWebApplication newApplication;
                    builder.Port = port;
                    builder.ApplicationPoolId = "SharePoint - " + port.ToString();
                    builder.IdentityType = IdentityType.SpecificUser;
                    SPFarmManagedAccountCollection manaccountcollection = new SPFarmManagedAccountCollection(SPFarm.Local);
                    SPManagedAccount maccount = manaccountcollection.FindOrCreateAccount(@UserName);
                    builder.ManagedAccount = maccount; //use the SPManagedAccount to receive the username and password automatically
                    builder.RootDirectory = new System.IO.DirectoryInfo("C:\\Inetpub\\wwwroot\\wss\\VirtualDirectories\\" + port.ToString());
                    builder.CreateNewDatabase = true;
                    builder.DatabaseName = DBName;
                    builder.DatabaseServer = ServerName;
                    builder.UseNTLMExclusively = true;
                    newApplication = builder.Create(); // Create new web application
                    newApplication.Provision();
                    
                }
                else
                {
                    MessageBox.Show("Port Not available");
                }

            }
            catch (Exception ex)
            {
                label1.Text = ex.Message;
            }



//Here the actual method for checking given port number is in use or not


        public static bool CheckPortUsage(int port)
        {
            try
            {
                new TcpClient(new IPEndPoint(IPAddress.Any, port)).Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

Monday, September 3, 2012

How To: Configure Forms Base Authentication in SharePoint 2010 (without opening web.config)


I have much researched about configuring Forms Based Authentication by viewing different blogs. After i succeed, i have to decided to write the details steps to configure FBA in SharePoint 2010(in an easy way without opening web.config file).


In MOSS 2007, it is required to configure the web.config file of the FBA site and Central Administration site. In SharePoint 2010, it is required to configure the web.config file of the FBA site, Central Administration site, and the Security Token Service (STS) web.config file. STS is one of the next generation Single Sign On services used to store credentials of an application in SharePoint 2010.

Below are the steps required to configure FBA in SharePoint 2010. I will be using MS SQL database as membership store for users.  

A) Setting up ASP.NET Forms Authentication User and Role Data Source 

1. Create Database 

2. Create User

B) Create Web Application and Site Collections

C) Configure Web.Config file 

1. Configuring FBA web application web.config file from IIS

2. Configuring Central Administration web application web.config file from IIS

3. Configuring Security Token Service web.config file from IIS

D) Adding User Policy to the FBA Web Application

E) Verification Steps.

A) Setting up ASP.NET Forms Authentication User and Role Data Source 

1.Create Database

Database is created using the ASP.NET SQL server setup wizard.

To open the wizard just type the following command in Run (windows+R)

"%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql"

Then it will open wizard 

i. Click on "Next".

ii. select "Configure SQL server for application services and click on "Next".

iii. Specify Server name , login details , and Database name. then click on "Next".
iv. Click on Next to create Database with specified Database name.

v. After successfully database creation is over click on "Finish".

vi. After creation of Database now we can check the database on server.

2. Create User

There are different ways to create aspnet users.

here am using one tool for create aspnet users, can download from here.

Once download the file unzip it and open "MembershipSeeder.exe" xml file and change server name ,database name then save the file and open "MembershipSeeder" application file and create users.

here the application interface for creation of users,roles.


Here am creating single user so selected "Only create or delete 1 user; dont use the # of Users field" check box. Then click on Create button.(other fields remains same);

We can check whether the user is created or not.

Open SQL server and select database then write the following query
"select * from aspnet_users".
 now u can find the user name in the result set.


Till now successfully created Database and aspnet users.

B) Create Web Application and Site Collections

i. Go to Central Administration and create new web application
ii. new web application's authentication is Claims Based Authentication and Check "Enable Forms Based Authentication(FBA) then specify ASP.NET Membership provider name and ASP.NET Role Manager Name. These two names will be used while adding providers to web applications in the next step.
Note: Membership provider name and Role Manager Name should be unique in all step while configuring FBA.


iii. After successfully creation of web application create site collection with any template.

C) Configure Web.Config file 

Now it is required to configure the web.config file of the FBA site, Central Administration site, and the Security Token Service (STS) web.config file. 

But here am not interested to open web.config file add connection string,sqlmembershipprovider and sql roleprovider details.

Simply i will open iis add connection string, membership provider and role provider to each web application namely FBA site (created in Step B), Central Administration site and the Security Token Service.

1. Configuring FBA web application web.config file from IIS

i. Type the following command "inetmgr" in Run(windows+R) and press enter.

ii. Select FBA site and double click on Connection string.

iii. Now click on Add.. link and specify connection string name, SQL server name and login details. Then click on "OK".





iv. Now select FBA site and Click on Providers.
v. From the Feature dropdown select .Net Roles and click on Add..
vi. Now specify
                      Provider Type: SqlRoleProvider, 
                      Name: SqlRoles(because while creating FBA site we specify RoleProvider name as SqlRoles so we have to use that name while adding RoleProvider), 
                     Connection string name: select name from dropdown 
                     ApplicationName:  "/" 


v. Now change Feature type to .Net Users and click on add..
vi. Now specify
                      Type: SqlMembershipProvider
                      Name: SqlMembers
                      ConnectionstringName:select name from dropdown which point 2 aspnetDB
                      ApplicationName: /



Till now we done configuration with FBA site follow the above same steps with central administration and SecurityTokenService.

2. Configuring Central Administration web application web.config file from IIS

i. Select central administration website and click on Connection String.
 ii. click on Add..
 iii. Specify details server name, database details
 iv. Now select central administration site and click on provider.
 v. Click on Add..
 vi. Specify
                Type: SqlRoleProvider
                Name: SqlRoles
                ConnectionStringName: select name from dropdown
                ApplicationName: /

 Now select .Net Users and click on Add..

 Specify as
                Type: SqlMembershipProvider
                Name: SqlMembers
                ConnectionStringName: Select from dropdown
                ApplicationName: /


3. Configuring Security Token Service web.config file from IIS


Repeat above step which are done in Step2

i.Select SecurityTokenService site and click on ConnnectionString

 ii. Click on Add..
 iii. Specify Server, Database login details and click "OK".
 iv. Now select STS site and Click Providers
 v. Select .Net Roles from Features dropdown and click on Add...
 vi. Specify

                Type: SqlRoleProvider
                Name: SqlRoles
                ConnectionStringName: select name from dropdown
                ApplicationName: /


 vii. Now change Features to .Net Users and click on Add...
 viii. Specify
                Type: SqlMembershipProvider
                Name: SqlMembers
                ConnectionStringName: Select from dropdown
                ApplicationName: /


D) Adding User Policy to the FBA Web Application

i. Go to Central Administration site --> Application Management
ii. select FBA site and click User Policy tab in Ribbon.

iii.Click on Add Users

iv. select zones as (All zones) and click on Next.


v. Type complete aspnet user and press(ctrl+K) then check "Full control" check box and finish.


E) Verification Steps

Till now FBA configuration is over now i want to check FBA is working for my FBA site or not.
i. Open FBA site in the browser and select "Forms Authentication".

ii. Enter Aspnet user complete name and password.

iii. if the FBA configuration successfully implemented then u should login to the site otherwise somewhere went wrong in the FBA configuration.


Thats its.

FBA configuration so easy, isnt it?

you can find all screenshots in a single file from here.
Pls feel free to send feedback to me.