Sunday, October 17, 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

Thursday, October 14, 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();