Pages

Monday, October 18, 2010

SharePoint Auditing – Behind Screen and Remedy



 Hi,
from past few days I was on my way (as usual) and exploring SharePoint Server 2007 Features and so came across Auditing.
as name suggests , Auditing in MOSS 2007 is basically keeping an eye over your target users actions. what I mean is you can audit the events like what end users has viewed , downloaded , checked in , checked out from your site. and more granularly this can be done at the Item Level and so you called ItemLevelAuditing in MOSS 2007. you can have a look at this link for primary idea how and what Auditing and reporting is done.
so I came to know that , the Audited data gets saved in the Content db of the site collection so I headed forward and opened SQL Server Management Studio. there you can see the db Table Name : AuditData
what was surprising me that If one configures MOSS to audit all the OOB supported events then this might blot up the size of content db and can cause some trouble for public facing sites where you don't know how many end users you going to have for your site
when you open AuditData table and try to see some entries then SharePoint adds the event types and item types in some columns as numbers
I opened up faithful buddy reflector to see what those really are and here you can find those
Item Types
    Document = 1,
    Folder = 5,
    List = 4,
    ListItem = 3,
    Site = 7,
    Web = 6
now, suppose each audit entry requires space appx 64Kb of size per request then supposing 10000 users per day, so at the end of the day content db size only for AuditData table will be at least 64000KB(64MB) which is quite large.
there need to be something to have control over this one : and Microsoft has provided a remedy
there is something called as TrimAuditLogs. this is a stsadm operation and gives control to farm administrators to maintain the audit entries. administrators can run this command and trim the audit logs older than provided date
stsadm -o trimauditlog –date 20080704 –databasename WSSContent123456 
Note that : This operation is available after the Infrastructure Update for Microsoft Office Servers is installed.
so what If you don't have installed the required update?
nothing to worry .. developer can always find a way .. :)
Microsoft’s SharePoint Server object model provides number of APIs where you can do the stuff as you want
here is something small what I tried to delete all the old audit log entries in the db


try
{
      using (SPSite site = new SPSite("http://yoursite
"))
       {
            SPAudit auditE = site.Audit;
            auditE.DeleteEntries(DateTime.Now.AddMilliseconds(1));    //Microsoft Recommended
            Console.WriteLine("Entries Deleted");
       }
}
 catch (Exception ex)
  {
       Console.WriteLine(ex.Message);
   }
 Note that : SPAudit.DeleteEntries is a audit event so delete Audit Log will be written in db even If you delete all entries and this entry will not be deleted

you can play further with these APIs and have fun :)
Refn: here

SharePoint Fields – AddingFieldAsXML strange?

Hi,

I was bit happy after knowing that we can also add FieldAsXml in SharePoint but when I came to know something strange while using this method then that made me a puzzle ..

what basically this all about is :

when we add any field using this method then SharePoint just forgets to set Internal Name of the Field ,
Bil Simser has come up with a work around which makes things OK

I also heard somewhere that this issue is still there with SharePoint 2010 APIs .. 
I would surely like to hands on it using SP2010 as soon as I touches SP 2010 (heavy Infrastructure ;)) environment

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