Wednesday, July 22, 2009

Sharepoint Methods : Concise definitions

  • WebPart.RenderWebPart Method (Microsoft.SharePoint.WebPartPages)-Renders the HTML for the body of a Web Part to the client.
Namespace: Microsoft.SharePoint.WebPartPages
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)

  • BaseFieldControl.CreateChildControls Method (Microsoft.SharePoint.WebControls) - Creates any child controls necessary to render the field, such as a label control, link control, or text box control.
    Namespace: Microsoft.SharePoint.WebControls
    Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)

    Tuesday, July 21, 2009

    WebPart Class - Microsoft.SharePoint.WebPartPages

    Provides the base class for creating Windows SharePoint Services Web Parts.

    Namespace: Microsoft.SharePoint.WebPartPages
    Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)

    eginning with Windows SharePoint Services 3.0, the SharePoint Web Part infrastructure is designed and built to lie on top of the Microsoft ASP.NET Web Part infrastructure. Web Parts that inherit fromSystem.Web.UI.WebControls.WebParts.WebPart are fully supported in Windows SharePoint Services (with the exception of user controls as generic Web Parts), and can be used not only in ASP.NET applications but also in Windows SharePoint Services applications, whether Windows SharePoint Services is involved or not. The Windows SharePoint ServicesWebPart class is part of an infrastructure that was designed specifically for SharePoint sites, and Web Parts that inherit from this class can be used only in SharePoint sites.

    When creating new Web Parts, you have the option of creating Web Parts that inherit fromSystem.Web.UI.WebControls.WebParts.WebPart (recommended) or Microsoft.SharePoint.WebPartPages.WebPart. The Windows SharePoint ServicesWebPart class exists primarily for the purpose of backward compatibility (Web Parts written for Windows SharePoint Services 2.0 continue to work in Windows SharePoint Services 3.0 without modification), and to provide a small set of features that are not available in the System.Web.UI.WebControls.WebParts.WebPart class.

    The set of features provided exclusively by Microsoft.SharePoint.WebPartPages.WebPart is as follows:

    • Cross page connections

    • Connections between Web Parts that are outside of a zone

    • Client-side connections (Web Part Page Services Component)

    • Data caching infrastructure, including the ability to cache to the database

    Best Practices for writing web parts in SharePoint

    This is one of the most important articles I have read lately:
    Best Practices for writing web parts in SharePoint(Compiled from various blogs and articles)

    1. The link below describes in more detail the practice elaborated in this point

    Using Disposable Windows SharePoint Services Objects


    What it explains is that when you develop web parts and use the sharepoint objects (like spsite and spweb) you have to dispose of them because the garbage collector wont.

    The article also contains code examples for the recommended three ways to do this:
    1. use "dispose"
    2. use "using"
    3. use "try, catch, and finally blocks"




    Example for using "using":

    String str;

    using(SPSite oSPsite = new SPSite("http://server"))
    {
    using(SPWeb oSPWeb = oSPSite.OpenWeb())
    {
    str = oSPWeb.Title;
    str = oSPWeb.Url;
    }
    }

    Example for using "try, catch, and finally blocks":

    String str;
    SPSite oSPSite = null;
    SPWeb oSPWeb = null;

    try
    {
    oSPSite = new SPSite("http://server");
    oSPWeb = oSPSite.OpenWeb(..);

    str = oSPWeb.Title;
    }
    catch(Exception e)
    {
    }
    finally
    {
    if (oSPWeb != null)
    oSPWeb.Dispose();

    if (oSPSite != null)
    oSPSite.Dispose();
    }

    Error Handling in SharePoint

    ErrorHandling is probably one of the most lightly taken aspects of coding web parts but if implemented the right way can save loads of time for the developer and make life simple and error free.
    Here goes an excellent article by a good coder that talks more about it:

    Introduction

    When delivering webparts you need to have an approach on how to handle errors so that the the webpart "dies" in a nice way and can also communicate the problem to the administrators in a controlled way. This article explains the approach I have used. The approach used here is to catch the error in CreateChildControl and then display the error during the rendering in LiteralControls.

    Webpart overrides

    If you inherit from System.Web.UI.WebControls.WebParts.Webpart you should override the following methods:

    1. CreateChildControls: called by the ASP.NET page Framework to notify server controls that use composition-based implementation to create any child controls that they contain in preparation for posting back or rendering.
    2. Render: renders the control to the specified HTML writer.

    Who should catch what we throw...

    The problem I have seen is that if you don't handle errors in a controlled way, the whole SharePoint page gets corrupt which is not acceptable. We should have the following goals of how we handle errors:

    1. Die in a graceful way
      If a webpart just throws an error, the Sharepoint page is not usable
    2. Report something back to the end user
      We need to tell the end user that we have a problem
    3. Send more error information for troubleshooting
      We need to have the possibility to have more extensive error reporting for the System Administrator...

    My Approach: Coding a webpart

    To at least "die" in a controlled way, I have implemented the following approach. I am still missing some good guidelines on how to do Tracing and Event logging inside Sharepoint 2007 from Microsoft.

    1. Create a private variable to store the Exception inside the webpart
      Collapse
      private Exception childException = null;
    2. In CreateChildControls, have a try/catch and catch the error in the childException variable
      Collapse
      try {     base.CreateChildControls();     ..... } catch (Exception exp) {     HttpContext ctx = HttpContext.Current;     ctx.Trace.Warn(ex.ToString());     childException = ex; } 
    3. In Render check using try/catch and catch the error in the childException variable and then display the result in the page by using labels..
      Collapse
      protected override void Render(System.Web.UI.HtmlTextWriter writer) {     if (childException != null)     displayErrorMsg(writer, childException);     try     {         ...         ...         ...     }     catch (Exception ex)     {         displayErrorMsg(writer, ex);     } 

      where

      Collapse
      private void displayErrorMsg(HtmlTextWriter writer, Exception ex) {     this.Controls.Clear();     this.Controls.Add(new LiteralControl(ex.Message));     if (ex.InnerException != null)     {         this.Controls.Add(new LiteralControl(ex.InnerException.Message));     }     this.Controls.Add(new LiteralControl());     base.Render(writer);     HttpContext ctx = HttpContext.Current;     ctx.Trace.Warn(ex.ToString()); }

    Why Site Collection in SharePoint

    I have an extract from Dave Wollerman's blog to explain the logical architecture of a Site Collection and why we need it.Here it goes:

    I feel that understanding the architecture around the definition and maybe the purpose of sites and site collections is the building blocks of SharePoint. Most people coming into SharePoint for the first time see it as if it were a single web application with a ton of features and settings. They also look at it as if the taxonomy of the sites were a physical heirarchy on a file system. Its hard for some people to grasp the understanding that everything is stored in a database and there is no real physical site hierarchy.


    SharePoint Architecture

    With the use of managed paths, site collections can impersonate a physical hierarchy for a means of organization, but the site collections are still on the same level as far as SharePoint is concerned. A Farm, Web Application, and a Site Collection are all "Containers" (or in active directory thinking "organizational units").

    As far as when to use a site collection as opposed to using a sub-site, that is based on the organization itself. Usually it is the IT department’s goal to implement the SharePoint system. In most circumstances these people are infrastructure experienced and not experienced in deploying applications. This results in a hodge-podge installation of SharePoint with a single site collection to house everything known within the organization. This will result in a failed installation, since there is no room for scaling. Plus IT will either be burdened with the task of maintaining all the structure and security, or they will demand it (sometimes IT doesn't get the idea of distributed security).

    Site collections will allow the IT department freedom to maintain just application itself without the worry of security or content hierarchy maintenance. The following is a list of what an individual site collection offers.

    For the Users:
    • Dedicated Recycle bins
    • Dedicated usage Reports
    • Distributed administration (site collection administrators)
    • Dedicated search scopes, keywords, and best-bets
    • Custom feature deployments
    • Dedicated language translation maintenance
    • Dedicated galleries for web parts, master pages, content types, site columns, site templates, and list templates
    • Dedicated shared libraries, such as site collection images and site collection styles
    • Dedicated real estate (Self Containment)
    For the IT Administrators:
    • Site quota templates
    • Distributed administration
    • Site locking
    • Database maintenance options
    • Backup / Restore abilities
    • Content Deployments
    • InfoPath forms services global template targeting

    I have 2 big, at least what I think is big, points on why to use site collections. The first one is site quotas and recycle bins. The issue is the recycle bin is based on site collections and the quota for a site collection. If everyone shares a site collection, then they share the recycle bins storage size. The example I usually give is HR deletes 1MB per day and IT deletes 1GB per day. With a 5GB site quota, HR content will be flushed through the system a lot quicker if they share a recycle bin. This results in having to restore a database to get back a single document. (You know it will happen, Murphy’s Law). If they were in separate site collections, then the HR recycle bin would be valid for months maybe years with a 5GB quota because it is only affected by their deletions.

    The second point is distributed administration. For most small companies this might be a moot point, but for high content driven organizations with a small IT force. It is a godsend for IT. IT doesn't know who should be able to see what content, besides how it should be organized. This is the job of the content owners and users. SharePoint site collections offers IT the ability to create a site collection for a project, team, department, document, or whatever the needs are, then assign an owner and hand it off to them. Now IT has time to work on IT related issues and the content owner now has their real estate to start developing. By this I mean, they now have the ability to quickly create libraries, calendars, meetings, wikis, whatever they feel they need to efficiently organize their content in a meaningful manner to the people who will be using it.... THEM. Plus the ability to allow users to either read or contribute to the site. This is huge since the content owners best know who needs to author, read, consume the content, plus how it needs to be organized. IT usually has no clue which then leads IT down a long time consuming road to interpret how the content owner needs it and will probably have to keep changing it constantly since they are the owner. IT has their own work to do as well.

    Monday, July 20, 2009

    Sharepoint Feature: Definition

    Windows SharePoint Services 3.0 introduces an inherently portable and modular functionality known as a feature, which simplifies modification of sites through site definitions. A feature is a package of Windows SharePoint Services elements that can be activated for a specific scope and that helps users accomplish a particular goal or task.

    Example of a Feature

    A Feature could provide, for example, a "My Favorite Items" functionality that includes the following elements:

    • A custom list that stores, per user, a list of favorite items, which is created as a single hidden list per workspace when the Feature is enabled.

    • A custom menu item that is attached to all lists, called "Add to Favorites," which adds an item to the Favorites list.

    • A Web Part that implements usage and link tracking to display the user's top 10 favorites at the top.

    Thursday, July 16, 2009

    Master Pages : Location

    The default master page is located in the Master Page Gallery. In Office SharePoint Designer 2007, the Master Page Gallery is the masterpage folder, which is located in the _catalogs folder in the Folder List.


    If you create customized master pages, it is a good idea to store them in the samemasterpage folder. That way, you can easily find and manage all of your master pages for the site in a single location.

    You can also identify the location of the master page to which any content page is attached by viewing the master page indicator in the upper-right corner of the content page.

    Identify the location and name of the master page by using the breadcrumb