Pages

Friday, October 15, 2010

SharePoint - AddingFieldAsXML

Hi,

I was basically trying to hook up a SharePoint Field to the one of document library of my site , I know there are different approaches like using UI, writing code.. that one can follow to add desired field directly
but here is something new I came across , Adding Field as XML..:)
what this approach is all about? this is like a creating a Site Column (Field Element) by using feature (I hope I am right)
we can  create a field on the fly and provide some attributes like ID, Type, DisplayName, Name, Required….
this is what I did for attaching a Note Type Field to one of doc Lib

using (SPSite site = new SPSite(http://YourSite))
{
   using (SPWeb web = site.RootWeb)
   {
      SPList docList = web.Lists["Documents"];
      if (docList != null)
      {
         XmlDocument doc = new XmlDocument();
         XmlElement xmlElement = doc.CreateElement("Field");
         xmlElement.SetAttribute("ID", "0F7A6C90-8715-4aa7-90FE-3491DC8953C7");   //ID can be any GUID        
         xmlElement.SetAttribute("Type", "Note");
         xmlElement.SetAttribute("Name", "UserNote");
         xmlElement.SetAttribute("DisplayName", "UserNote");
         xmlElement.SetAttribute("Required", "FALSE");

         docList.Fields.AddFieldAsXml(xmlElement.OuterXml);
         docList.Update();
                           
         Console.WriteLine("Added Field As XML");
      }
   }
}

SharePoint - Add Audience Rule Programatically

Hi,
I know this is not something special I am posting in this one because this is something which is easily available on internet if you try to use our Google / bing correctly :)
I was working (rather having a look at) Audience Targeting Feature in SharePoint 2007 and came to know that somehow we can add only six rules while creating a audience by using user interface (which you can found in SSP) here is link how you can do that
but after reading somewhere, I figured out the way to add more than six audience rules .. Yes you are right.. From code
this might be because they thought , generally one cant  make such a complex audience having more than six audience rules and I also thinks so :)
but still If one want to have a look at API and try to add more than six audience rules then this is how I done it (nothing special , just added a rule using API..You can concatenate such chain of rules using AND , OR)

try
{
  using (SPSite site = new SPSite(http://yoursite))
  {
    ServerContext siteContext = ServerContext.GetContext(site);
    AudienceManager aManager = new AudienceManager(siteContext);
    AudienceCollection audiences = aManager.Audiences;

    Audience INFORMANAGEMENTUSERS = null;

    if (audiences.AudienceExist("Information Management Users"))
    {
      INFORMANAGEMENTUSERS = audiences["Information Management Users"];
      Console.WriteLine(string.Format("Audience Found with Name {0}", INFORMANAGEMENTUSERS.AudienceName));
    }
    else
    {
      INFORMANAGEMENTUSERS = audiences.Create("Information Management Users", "Test Audeinces");
      Console.WriteLine("Audience Added");
    }

      ArrayList aRules = INFORMANAGEMENTUSERS.AudienceRules;

      if (aRules == null)
      {
         aRules = new ArrayList();
      }
      else
      {
        AudienceRuleComponent rule1 = new AudienceRuleComponent(null, "AND", null);                           //Just Concatenating Previous Rule
        aRules.Add(rule1);
      }

AudienceRuleComponent rule2 = new AudienceRuleComponent(PropertyConstants.Department, "=", "Information Management");

aRules.Add(rule2);

       INFORMANAGEMENTUSERS.AudienceRules = aRules;
       INFORMANAGEMENTUSERS.Commit();

       Console.WriteLine("Audience Rule Added");

   }
 }
   catch (Exception ex)
   {
        Console.WriteLine(ex.Message);
   }

   Console.ReadLine();

Friday, August 6, 2010

Configuring MOSS 2007 Anonymous site to use ASP.NET Membership Provider

Hi again in this anonymous site series..

In my previous post I explained steps about how to create anonymous web site using SharePoint existing site. So in this post I am going to make use of out of the box ASP.NET Membership Provider and it to anonymous site which we created. (This is easy one)

So very first step is to have ASPNET db in SQL Server, and for this we are going to use the traditional ASP.NET way with popular command aspnet_regsql

1. Open visual studio command prompt and type in command aspnet_regsql, you will see ASP.NET Sql Server setup wizard, click next and select configure SQL Server for application services.

Enter Database Server name and keep database name as <default> in dropdown (this will create db with name aspnetdb in SQL Server), click finish on next screen

2. Once aspnetdb database is created in SQL Server, then next step is to make use of this db.

Browse to Central Administration site and click on Application Management tab. Find and click on Authentication Providers, select Internet zone. Change Authentication type to Forms , and enter Membership Provider Name as AspNetSqlMembershipProvider and Role Manager Name as AspNetSqlRoleProvider. Click on save.


3. Now we have configured anonymous application to use Forms authentication, next step is to specify Membership provider and role manager entries in web.config file of anonymous web site

Add following entries to web.config file


 
<membership>
  <providers>
   <add
name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider,System.Web,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
passwordFormat="Hashed"
applicationName="/"
/>
  </providers>
</membership>

<roleManager>
  <providers>
    <remove name="AspNetSqlRoleProvider" />

<add
name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider,System.Web,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer"
role="SignedInClient"
applicationName="/"
/>
   </providers>
</roleManager>

<connectionStrings>
   <remove name="LocalSqlServer" />

<add name="LocalSqlServer"
connectionString="Data Source=DbServer;Initial Catalog=aspnetdb;
Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

Connection string is required for application to know about aspnetdb.

4. Now , to register users with anonymous site you can use out of box ASP.NET user registration wizard control and Login control , so there are two ways to achieve this
a. You can create your custom page layouts and add those ASP.NET controls to page layouts like this

For Login Page:
<asp:Login runat="server" ID="Login1" DestinationPageURL="/Pages/Default.aspx" CreateUserText="Create User" CreateUserUrl="/Pages/Registration.aspx"></asp:Login>
For Registration Page:
<asp:CreateUserWizard ID="CreateUserWizard2" runat="server" ContinueDestinationPageUrl="/Pages/Default.aspx">
            <WizardSteps>
       <asp:CreateUserWizardStep ID="CreateUserWizard1" runat="server" />
       <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server" />
            </WizardSteps>
 </asp:CreateUserWizard>


b. Or you can create your web parts wrapping OOB ASP.NET controls and add them to your site

Login WebPart :
             protected override void CreateChildControls()
            {
            try
            {
                base.CreateChildControls();
                Login loginControl = new Login();
                loginControl.DestinationPageUrl = "/Pages/Default.aspx";
                loginControl.CreateUserUrl = "/Pages/Registration.aspx";
                this.Controls.Add(loginControl);
            }
            catch (Exception ex)
            {
               
   }
Registration WebPart:
protected override void CreateChildControls()
        {
            try
            {
                base.CreateChildControls();
                CreateUserWizard cwz = new CreateUserWizard();
                cwz.ContinueDestinationPageUrl = "/Pages/Default.aspx";
                this.Controls.Add(cwz);

            }
            catch (Exception ex)
            {
               
            }
        }


Note: there are many properties of these controls which you can configure as your need



5. Once you have set up all these things and have your custom login page then , change default login page of anonymous site using IIS Manager like this

Find anonymous site in IIS > find authentication in features view >right click on Forms Authentication> click edit > change login url



Above all settings worked pretty perfectly for me (at least :) )

Reference best Links: This and This

Monday, June 28, 2010

Exploring SharePoint Publishing Feature to Configure From Onet.xml

When you create your own publishing site definition then you can configure SharePoint out of the box publishing feature in onet.xml


Example: when you open onet.xml of existing publishing site definition then you find publishing feature configured like this:

<Feature ID="22A9EF51-737B-4ff2-9346-694633FE4416">


<Properties xmlns="http://schemas.microsoft.com/sharepoint/">


<Property Key="ChromeMasterUrl" Value=""/>


<Property Key="WelcomePageUrl" Value="$Resources:cmscore,List_Pages_UrlName;/default.aspx"/>


<Property Key="PagesListUrl" Value=""/>


<Property Key="AvailableWebTemplates" Value=""/>


<Property Key="AvailablePageLayouts" Value=""/>

<Property Key="SimplePublishing" Value="true" />


</Properties>


</Feature>

So you can obviously copy and paste this OOB configuration to use in your custom site definition,

So as curiosity I was googling for more options using which we can configure publishing feature , in fact I was trying to explore more properties of this feature and I came to know that there are many more properties available using which you can configure this feature, like : Scheduling of pages , versioning, workflow settings of pages library.

AlternateCssUrl


AvailablePageLayouts


AvailableWebTemplates


ChromeMasterUrl


EnableApprovalWorkflowOnDocuments


EnableApprovalWorkflowOnImages


EnableApprovalWorkflowOnPages


EnableModerationOnDocuments


EnableModerationOnImages


EnableModerationOnPages


EnableSchedulingOnDocuments


EnableSchedulingOnImages


EnableSchedulingOnPages


MigrationOverride


PagesListUrl


RequireCheckoutOnDocuments


RequireCheckoutOnImages


RequireCheckoutOnPages


SimplePublishing


VersioningOnDocuments


VersioningOnImages


VersioningOnPages

Refn: Sezai’s blog and this beautiful link

Creating Anonymous Site using SharePoint Existing Web Application

From long time I was looking after this as curiosity, and now after some google I have understood how this can be done, I will be summarizing points and procedure which is used to create anonymous site by using existing SharePoint web application

This is the scenario which is most useful when you want to use only one content database and have two applications, one application will be working as CMS (Content Management System) and other will be like public facing Internet site. So in such cases content creators, site administrators will be adding /modifying contents and then those will be reflected on public facing site.

So let’s start by creating a SharePoint Web Application, which we will be using as CMS site

1. Go to central Administration and click on Application Management Tab

2. Find option to create or extend web application and click on it

3. After clicking , you will see two options like create or extend web application , select create new

4. Now create new application on any port which you want with and be careful while configuring security , select Authentication Provider as NTLM and Allow Anonymous access to NO



5. Provide valid user name and password and click on create. , then after creating web application create root site collection with Publishing Portal Template


Now browse to the root site collection and click on site settings and then click on modify all site settings

In People and Groups section, click ion Advanced Permissions, then in this list click on settings, and then click on Anonymous Access and then select Entire Web Site.

(Note: If you are not able to see anonymous access option anymore after clicking on settings then do the following settings

a. Open central administration and click on application management tab

b. Find Authentication Providers option and click on that (Under Application Security Section)

c. Select Default Zone and select Anonymous Access to true

d. Then go back to your CMS site and again go to advance permissions , settings , now you will see anonymous access , select entire web site and click ok

e. Now go to authentication provider and set anonymous access in default zone to false which you set to true in step c , and click ok )

So as of now we have created and configured our CMS site, now we will extend this created web application for anonymous access.

1. Open Central Administration again and click on Application Management Tab

2. Click on create or extend web application

3. Now click on extend existing web application

4. Select web application which we just created in Web Application section

5. In IIS Website section , select create new IIS Site and give port number as 80 . (Note: you can give any port number which you want and also host header as you wish)

6. Now In security configuration settings section, do the settings as , select allow anonymous as Yes


In Load balance URL section, select zone as Internet and click ok to extend web application.

Now we have extended CMS web application for anonymous access.

If you open IIS Manager, there you will see this site created

(To Enable Forms Authentication on this anonymous site do the following steps or open this link)

Open IIS Manager and click on anonymous site, go to Authentication

Set Anonymous Authentication as Enabled and Forms Authentication as Enabled and all other as Disabled

(Note: when you enable Forms Authentication on extended site then you must provide name of Membership Provider and name of Role Provider , for example : you can use ASP.NET OOB membership and role providers but you should have to have membership services configured on SQL Server , this link and this link explains more on this)

And you are done :)

Try browsing this site and enjoy..