Monday, July 16, 2007

Creating a site using object model in forms authentication

Recently we had new requirement to create a blog in share point site from Asp.net Application.
The Share Point site is in Forms authentication. To Create the blog site
public void CreateSite(string strSiteURL,string strSiteTitle)
{
//Create a new SPSite object corresponding to your top level site
SPSite newSite = new SPSite(“http://moss/site”);
//Create a new SPWeb object from the newSite object
SPWeb newWeb = newSite.OpenWeb();
//Return the collection of sub sites
SPWebCollection subSites = newWeb.Webs;
If the site is blog site the template id is "Blog#0"
SPWeb newSubWeb = subSites.Add(“newsite”, “New Site”, “This is the description for my new site.”, 1033, “Blog#0”, true, false);
}
Remember it works in windows authentication, but it fails in forms authentication.
Because forms authentication doesn't have the enough authentication to create a site. It should be Impersonated with windwos credentials.

  • go to central administration ->application management ->Create or Extend Web Application
  • click on 'Extend an existing Web application' select the web application for which you enabled forms authentication.create a new IIS Website with zone as Intranet. for ex: http://systemname:1234/
  • create an Asp.net webservice(for ex: myService) project and map it to the virtualdirectory of the above port.
  • http://systemname:1234/myService.
  • In the webservice Paste the above method.
  • Add this webservice from asp.net application/webpart as below
  • If the webreference is CustdomSite then the code as follows
  • The code in asp.net application
  • Public void CreateSite()
    {
    CustdomSite obj=new CustdomSite ();
    System.Net.NetworkCredential cred = new NetworkCredential(UserName,Password,Domain); [Import system.Net namespace]
    obj.Credentials=System.Net.CredentialCache.DefaultNetworkCredentials;
    obj.CreateSite(txtsiteUrl.Text,txtSiteName.Text);
    }
    Assuming the user knows the Creation,deployement of custom webparts
  • Important Note: Most of the Object model functionalities in Forms authentication one needs to follow the above process.

Separate css file for each individual site

By Default each site takes the default style as Core.css file (Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033\STYLES\core.css).
If we included our custdom classes in core.css that would applied to all sites available in the system. Bcoz its global for all sites in the system.
To apply Site specific Css style
Site Actions -> Site Settings -> Master Page
by default "Use Windows SharePoint Services default styles" is selected. select "Specify a CSS file to be used by this publishing site and all sites that inherit from it" and specify the location of the Css file. Here the changes that you made will be applicable to that particular site only.

Saturday, July 14, 2007

How Forms Authentication is differ from Windows Authentication in adding users to the Group in object model

When you have forms authentication enabled for your site and adding users to Group is not as simple as you do with windows authentication in object model. In windows authentication to add the users to the group(assumption: the current domain user has the permission to add the user to group) we have In-built webservice http://server/site/_vti_bin/UserGroup.asmx and using AddUserToGroup method from it.
note: here the site should be any windows authentication site.
. But in forms authentication its not going to be work. The reason is that the forms tries to access the http://server/site/_vti_bin/UserGroup.asmx service it will redirect to the login page of the current working site. Bcoz the user is not windows impersonated so he doesn't has the permission to access. The work around is to impersonate webservice with user windows domain credentials. If the webreference name is "GroupUser" follow below steps:

  • GroupUser obj=new GroupUser();
  • System.Net.NetworkCredential cred = new NetworkCredential(UserName,Password,Domain); [Import system.Net namespace]
  • obj.Credentials=System.Net.CredentialCache.DefaultNetworkCredentials;
  • Now call the method as obj.AddUserToGroup(groupName As String,userName As String,userLoginName As String,userEmail As String,userNotes As String)

This will work out.

Sunday, July 8, 2007

Moss 2007 Search in Forms Authentication Mode

The out of box feature of Moss 2007 search doesn't works in Forms authentication mode.
To get rid of this problem
go to central administration ->application management ->Create or Extend Web Application
click on 'Extend an existing Web application' select the web application for which you enabled forms authentication.
create a new IIS Website with zone as Intranet. for ex:
http://systemname:1234/
now go to shared service administration, create new shared service provider(SSP).
go to SSP->search settings click on Content sources and crawl schedules, click on link
Local Office SharePoint Server sites. In the site addresses box add Intranet site(http://systemname:1234/)
check start full crawl of this content source checkbox and click on ok. It take few minutes to crawl all content of your Site.
That's it now you can search forms word in your forms authentication site.....
To crawl the new content of the site you can set the incremental crawl duration in Local Office SharePoint Server sites.

Saturday, July 7, 2007

Moss 2007 Forms authentication

As we know that by default Share point picks the users list from the ActiveDirectory(Windows Authentication). we dont have forms authentication concept in sps3 it was introduced in Moss 2007. To enable forms authentication follow the below steps.

Create aspnetdb database to store all our roles and users. You will find this database installed at 'c:\windows\microsoft.net\framework\v2.0.50727' if you have Visual Studio 2005 installed in your machine. go there and run aspnet_regsql. To Create your roles and membership data on the sql server you need to edit web.config for the site.we need to supply the connection string details for our aspnetdb. Just outside the system.web tag put:




and inside the system.web put the following code










Once you’ve edited both web.config’s go to a command prompt and do an iisreset.
Now we need to add some users to our database. The easiest way to do this is to create an ASP.NET 2.0 website using Visual Studio 2005 (or Visual Web Dev). Add a web.config to the project and then add exactly the same connection string as you did above for your new SharePoint site. Build the project.

Back in Visual Studio, under the website menu, select the option for ASP.NET Configuration. This opens up the ASP.NET web site administration tool where we can add the users and roles we want.
Select the Security link, create a role (something like administrators), and then go and add a user.

Now go to Application Management page in Central Admin and click on Authentication Providers. select the site you wish to change the authentication provider to Forms for. Once you've selected it you'll see Windows as the MemberShipProviderName.
Set the authentication type to Forms and enter the Member Ship Provider Name to AspNetSqlMembershipProvider. If anonymous access isn't ticked, tick that now.
Goto Central Administration -> Application Management -> site collection administrators – and enter the username created by the asp.net application.

Now when you try login into the site will redirect to the login page, where you need to provide the credentials of the user that you have created.