FB_init

Monday, November 21, 2011

My daily SharePoint frustration - XXI

Scenario: in a site with both forms and NTLM authentication, a user would add a List View Web Part to the page. The user would try to delete a document from it and an error appeared on the browser:



The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.


What I tried:
  I tried to add this to the custom master page (just before the closing form tag):



        

It didn't work. I saw no difference in the source html. And I noticed that the form digest information was always there, regardless.
  I also tried to add to the existing code behind of the custom master page:

  protected override void OnInit(EventArgs e)
        {
            if (Page.IsPostBack) // custom stuff 
           {
                Microsoft.SharePoint.Utilities.SPUtility.ValidateFormDigest();
            }

It didn't work.

I tried to remove my customized master page and to use an out-of-the-box SharePoint master page. The problem was still there. I then tried to use an out-of-the-box page layout. The problem was still there. I tried to create a brand new out-of-the-box web application. The problem was not there. I then went back to web site where the problem was happening and began to comment out the http modules in web.config - trying different combinations to isolate the HTTP module. And I zoomed in to one module. I also changed the order of the http modules. Still, the same http module was the one.
Even after I found the faulty http module, it was not easy to isolate the problem.  I had to comment out the different conditions until I isolated SPContext.Current. 

Solution:

( It depends on your environment... It may not be applicable to you )  
Changing the HTTP module so that the evaluation of SPContext.Current only happens after the verification that the current handler is a Page. :

   public class MyMengaoModule : IHttpModule

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute +=
            new EventHandler(ContextPreRequestHandlerExecute);
        }

        void ContextPreRequestHandlerExecute(object sender, EventArgs e)
        {
            Page page = HttpContext.Current.CurrentHandler as Page; // new stuff

            if (page == null)
            {
                return;
            }

            if (SPContext.Current != null && SPContext.Current.Site != null && SPContext.Current.Site.Zone == SPUrlZone.Internet) // as it was before
            ...

Time it took me to fix the problem: 3 days of work...


Wednesday, November 16, 2011

Permission mask utility for SharePoint (2010)

Here is an utility for translating permission masks in hex to SPBasePermissions values.Click below to download.





The core of the thing:

private void button1_Click(object sender, EventArgs e)
        {
            string userValue = textBox1.Text;
            listBox1.Items.Clear();
            long valueOriginal = long.Parse(userValue, NumberStyles.AllowHexSpecifier);
            long value = valueOriginal;
            int rbit = 0;
            do
            {
                int i = (int)(value % 2);
                long bitcheck = 1 << rbit;
                if (i > 0) // it matches
                {
                    string name = "" + ((SPBasePermissions)bitcheck);
                    string formatString = String.Format("{0,10:X}", bitcheck);
                    Console.WriteLine(name + " " + formatString);
                    listBox1.Items.Add(name + " " + formatString);
                }

                value = value >> 1;
                rbit++;
            } while (value > 0);
        }

A screenshot:

Monday, November 14, 2011

My daily SharePoint frustration - XX

(With SharePoint 2010)

The actions:
 - I edited a page to add a document library to it. When I tried to click on an item's menu (the ECB), I got an error dialog  saying "This item is no longer available. It may have been deleted by another user. Click 'OK' to refresh the page. "

I tried different things:

  - I couldn't use SharePoint Designer. It failed to edit any page in my case. So I couldn't follow these instructions to substitute ListViewWebPart  with XSLTListViewWebPart.

- I changed the code and assigned the website's ID to ListViewWebPart.WebID. That made no difference for me. (It worked for some other people. Probably because the underlying problem is different).

- I installed this hotfix: http://support.microsoft.com/kb/2405789 . It didn't work.

What worked for me was to edit the web part and uncheck some options (not sure exactly which one):

- Toolbar type: None (this alone may do the trick)
- Checked Enable Asynchronous Load, Enable Asynchronous Update, Show Manual Refresh Button and Enable Asynchronous Automatic Refresh.
- I unchecked Enable Data View Caching.