Pages

Thursday, January 20, 2011

Security Validation for this page is invalid......

I was working with SharePoint 2010 Modal dialogs for first time and was showing my custom application page in modal dialog
My application page was using custom master page and there were some buttons on my custom application page which were causing post back and doing some changes in user profile (basically accessing SharePoint site)
After everything was set up and I was ready to test.. and guess what .. After clicking on those buttons I was getting error like .. Security Validation of this page are invalid.. after some google I quickly got some results about this error and some post were quite useful
Firstly I got some posts which were describing about this error like, we should add web.allowunsafeupdates = true; in the code but this was not the case in my situation
After some more results I found that we should have to have <SharePoint:FormDigestControl> included on our page layout / master page and error was solved
You need to register assembly like this
<%@ Register
   Tagprefix="SharePoint"
   Namespace="Microsoft.SharePoint.WebControls"
   Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
And then add control
There is already placeholder for this control in SharePoint master page by default, but If you are using your own master page then you should make sure that you add this control
<SharePoint:FormDigest ID="MyFormDigest" runat="server"/>
What this control is all about?
Well.. msdn says that,
The FormDigest control generates a security validation, or message digest, to help prevent the type of attack whereby a user is tricked into posting data to the server without knowing it. The security validation is specific to a user, site, and time period and expires after a configurable amount of time. When the user requests a page, the server returns the page with security validation inserted. When the user then submits the form, the server verifies that the security validation has not changed.
For more Information: Here

Creating List Using List Definition Programatically



Let’s have a look at how we can create custom list by using our own List Definition
There is <ListTemplate> element using which we can easily create our own list definition.          Andrew Connel has explained this very well in this post
But this is something different approach I wanted to use with our own list definitions
After creating List Definition I didn’t want to add List Instance to the solution because if we do so, then it creates a list with based definition automatically when feature gets activated.
I wanted to have a control over creation of this list which will be based on our custom List Definition. Which puzzled me..
Then I found a way to achieve this by using object model.. I don’t know yet If there are multiple ways to do this but this one was perfect choice for me and worked very well J
When we create a List Definition then we have a feature which provisions list definition
Note that GUID used here in code is Feature Id of the feature which provisions the list definition.
Also : please take into consideration that there are some methods in server object model available for creating a list using list templates directly , but this was something different than list templates, so this approach was preferred , because List Templates are stored into the content database while list definitions resides on actual file system of the server


try
{
  SPList list = null;
  using (SPSite site = new SPSite("http://yoursite/"))
  {
     using (SPWeb web = site.RootWeb)
     {
      //List Will be Created Based on this ListDefinition
- OOB Custom List Definition
      //00BFEA71-DE22-43B2-A848-C05709900100

        foreach (SPList _list in web.Lists)
        {
          if (_list.Title.Equals("TestList"))
          {
              list = _list;
          }
        }

        if (list == null)
        {
         web.AllowUnsafeUpdates = true;
         Guid listID = web.Lists.Add("TestList", //List Title
                      "This is Test List",      //List Description
                      "Lists/TestList",         //List Url
                      "00BFEA71-DE22-43B2-A848-C05709900100", //Feature Id of List definition Provisioning Feature – CustomList Feature Id
                      100,                     //List Template Type
                     "101");      //Document Template Type .. 101 is for None

           web.Update();
           web.AllowUnsafeUpdates = false;
                           
         }
        }


     }
    }
    catch (Exception ex)
    {
      
    }

this is the way to do this and works fine :)
I hope some one finds this useful..