Sharepoint Debug Settings

If you want to open debug messages for a sharepoint application
edit your application web.config file.

[Drive]:\inetpub\wwwroot\wss\VirtualDirectories\[YourSite]\web.config

Make CallStack true: 
<SafeMode MaxControls="200" CallStack="true" DirectFileDependencies="10" 
TotalFileDependencies="50" AllowPageLevelTrace="false">

Set debug true:
<compilation batch="false" debug="true">

set mode off:
<customErrors mode="Off" />

That’s all

Disable Internet Explorer Enhanced Security in Windows Server 2008

When you are working with ie8 in windows server 2008 Enhanced Security feature make it very tedious and cause waste of time. You can disable it easily

  • Open the Server Manager by going to Start > Server Manager
  • In the Server Manager browse to the Security Information section and click Configure IE ESC.
  • Opening windows select OFF for administrators.

  • Open the Local Security Policy.(Windows Server 2008s Start button, then type secpol.msc in the Start Search dialog box)
  • Expand the Local Polices folder.
  • Drill down to Security Options folder.
  • Scroll down, and locate the family of settings beginning with ‘User Account Control’.
  • Focus on: User Account Control: Behaviour of the elevation prompt for administrator.  Double click and set to: Elevate without prompting. 
  • Restart you Windows Server 2008 computer.
  • Thats all.

    ASP.NET store Viewstate data in Session

    Consider you have a web control as an assembl y and you dont have the source code of this . And the control store its data in viewstate and that data is really big. You can not able to change viewstate usage for this control . And much worse you have to use this important control.Thats may be help you to reduce output and use viewstate process at same time.

    I mean you can use viewstate and store in session and reduce the weight of output.Ok that cost as server resources. And I dont really discuss it is proper or efficent or not. I Just show how to do it.

    My other article about viewstate deal with compression and how to override base page functions of
    LoadPageStateFromPersistenceMedium and SavePageStateToPersistenceMedium
    I also use that functions in this article.
    https://blog.bugrapostaci.com/2010/02/02/asp-net-compress-viewstate/

    We are using one unique session key as path  for each page in session like
     Session[“ViewState_” + this.Request.Path]
    You should better if you define a specific Guid value in your pages and use it for Session key.

    protected override object LoadPageStateFromPersistenceMedium()
    {
        string viewState = string.Empty;
        // try to get state data form Session.
        viewState = Session["ViewState_" + this.Request.Path] != null ? Session["ViewState_"
                          + this.Request.Path].ToString() : string.Empty;
     
        // Decompress the encoded and compressed state data than return it.
        byte[] bytes = Convert.FromBase64String(viewState);
        bytes = Utilities.MemoryCompressor.Decompress(bytes);
        LosFormatter formatter = new LosFormatter();
        return formatter.Deserialize(Convert.ToBase64String(bytes));
    }
    protected override void SavePageStateToPersistenceMedium(object state)
    {
        //Encode state data
        LosFormatter formatter = new LosFormatter();
        StringWriter writer = new StringWriter();
        formatter.Serialize(writer, state);
        string viewStateString = writer.ToString();
        byte[] bytes = Convert.FromBase64String(viewStateString);
        bytes = Utilities.MemoryCompressor.Compress(bytes);
        //Save to session
        if (Session["ViewState_" + this.Request.Path] != null)
            Session["ViewState_" + this.Request.Path] = Convert.ToBase64String(bytes);
        else
            Session.Add("ViewState_" + this.Request.Path, Convert.ToBase64String(bytes));
    }
    Happy Codding...

    ASP.NET Compress ViewState

    Short definition of ViewState from MSDN:
    Microsoft® ASP.NET view state, in a nutshell, is the technique used by an ASP.NET Web page to persist changes to the state of a Web Form across postbacks. In my experiences as a trainer and consultant, view state has caused the most confusion among ASP.NET developers. When creating custom server controls or doing more advanced page techniques, not having a solid grasp of what view state is and how it works can come back to bite you. Web designers who are focused on creating low-bandwidth, streamlined pages oftentimes find themselves frustrated with view state, as well. The view state of a page is, by default, placed in a hidden form field named __VIEWSTATE. This hidden form field can easily get very large, on the order of tens of kilobytes. Not only does the __VIEWSTATE form field cause slower downloads, but, whenever the user posts back the Web page, the contents of this hidden form field must be posted back in the HTTP request, thereby lengthening the request time, as well.

    The Cost of View State and Why we should consider to compress it:
    Nothing comes for free, and view state is no exception. The ASP.NET view state imposes two performance hits whenever an ASP.NET Web page is requested:

    1. On all page visits, during the save view state stage the Page class gathers the collective view state for all of the controls in its control hierarchy and serializes the state to a base-64 encoded string. (This is the string that is emitted in the hidden __VIEWSTATE form filed.) Similarly, on postbacks, the load view state stage needs to deserialize the persisted view state data, and update the pertinent controls in the control hierarchy.
    2. The __VIEWSTATE hidden form field adds extra size to the Web page that the client must download. For some view state-heavy pages, this can be tens of kilobytes of data, which can require several extra seconds (or minutes!) for modem users to download. Also, when posting back, the __VIEWSTATE form field must be sent back to the Web server in the HTTP POST headers, thereby increasing the postback request time.

    Where is in the Pipeline ?
    The view state is serialized to the hidden form field in the Page class’s SavePageStateToPersistenceMedium() method during the save view state stage, and is deserialized by the Page class’s LoadPageStateFromPersistenceMedium() method in the load view state stage. With just a bit of work we can have the view state persisted to the Web server’s file system, rather than as a hidden form field weighing down the page. To accomplish this we’ll need to create a class that derives from the Page class and overrides the SavePageStateToPersistenceMedium() and LoadPageStateFromPersistenceMedium()methods.

    After some boring information  lets Compress our  ViewState

    First for compressing a byte array add this code to your project.

    using System;
    using System.Text;
    using System.IO;
    using System.IO.Compression;
     
    public static class MemoryCompressor
    {
        /// <summary>
        /// Compress given data with Gzip
        /// </summary>
        /// <param name="unCompressedData">the data to compress as byte array </param>
        /// <returns>Compressed data as byte array </returns>
        public static byte[] Compress(byte[] unCompressedData)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                //Select as compress
                CompressionMode cmode = CompressionMode.Compress;
                //Compress Data dont forget to leave open true..
                using (GZipStream gzip = new GZipStream(ms, cmode, true))
                {
                    gzip.Write(unCompressedData, 0, unCompressedData.Length);
                }
                return ms.ToArray(); //Return as byte []
            }
        }
     
        /// <summary>
        /// Decompress given data with Gzip
        /// </summary>
        /// <param name="compressedData">the data to decompress as byte array </param>
        /// <returns>Decompressed data as byte array </returns>
        public static byte[] Decompress(byte[] compressedData)
        {
            GZipStream gzip;
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(compressedData, 0, compressedData.Length);
                // Reset the memory stream 
                ms.Position = 0;
                //Decompress
                CompressionMode mode = CompressionMode.Decompress;
                //Get Stream 
                gzip = new GZipStream(ms, mode, true);
     
                using (MemoryStream outMs = new MemoryStream())
                {
                    // 4096 bytes as buffer gives you best performance..
                    byte[] buffer = new byte[4096];
                    int byteReaded = -1;
                    byteReaded = gzip.Read(buffer, 0, buffer.Length);
                    while (byteReaded > 0)
                    {
                        //write to output stream
                        outMs.Write(buffer, 0, byteReaded);
                        //keep going...
                        byteReaded = gzip.Read(buffer, 0, buffer.Length);
                    }
                    gzip.Close();//Dont forget to close it.
                    return outMs.ToArray(); //return as byte array
                }
            }
        }
    }

    Add this code to your Page or BasePage code

    public class BasePage:System.Web.UI.Page
    {
        ...
        protected override object LoadPageStateFromPersistenceMedium()
        {
            string viewState = string.Empty;
            // try to get state data form RequestForm.
            viewState = Request.Form["__COMPRESSEDVSTATE"];
            // Decompress the encoded and compressed state data than return it.
            byte[] bytes = Convert.FromBase64String(viewState);
            bytes = Utilities.MemoryCompressor.Decompress(bytes);
            LosFormatter formatter = new LosFormatter();
            return formatter.Deserialize(Convert.ToBase64String(bytes));
        }
        protected override void SavePageStateToPersistenceMedium(object state)
        {
            //Encode state data
            LosFormatter formatter = new LosFormatter();
            StringWriter writer = new StringWriter();
            formatter.Serialize(writer, state);
            string viewStateString = writer.ToString();
            byte[] bytes = Convert.FromBase64String(viewStateString);
            bytes = Utilities.MemoryCompressor.Compress(bytes);
            //Save to page hidden field
            ClientScript.RegisterHiddenField("__COMPRESSEDVSTATE", Convert.ToBase64String(bytes));
        }
        ...
    }

    Note: You always consider that you need viewstate if not you should disable it or disable controls viewstates that not need to be store data in viewstate. And also there is a better way to use compress data that HttpCompress look for it.
    Another artice of mine about storing viewstate data in Session:
    https://blog.bugrapostaci.com/2010/02/04/asp-net-store-viewstate-data-in-session/

    For more information about viewstate
    http://msdn.microsoft.com/en-us/library/ms972976.aspx

    May the code be with you..

    32-bit flag struct example named BitFlagVector32

    An example of Flag struct that using interger and set by bits

        public struct BitFlagVector32
        {
            private int data;
            public BitFlagVector32(int data)
            {
                this.data = data;
            }
            public int IntegerValue
            {
                get
                {
                    return this.data;
                }
                set
                {
                    this.data = value;
                }
            }
            public bool this[int bit]
            {
                get
                {
                    return ((this.data & bit) == bit);
                }
                set
                {
                    int data = this.data;
                    if (value)
                    {
                        this.data = data | bit;
                    }
                    else
                    {
                        this.data = data & ~bit;
                    }
                }
            }
     
     
            public void Set(int bit)
            {
                this.data |= bit;
            }
            public void Clear(int bit)
            {
                this.data &= ~bit;
     
            }
     
     
        }
     
        public static class UsageClass
        {
            public static BitFlagVector32 flag = new BitFlagVector32(15);
            public static void Usage()
            {
     
                flag.Clear(0x00);  //No effect
                flag.Clear(0x01);  //value 14  00001110
                flag.Clear(0x02);  //value 12  00001100  
                flag.Clear(0x04);  //value 8   00001000
                flag.Clear(0x08);  //value 0   00000000
     
                flag.Set(0);        //No effect
                flag.Set(0x01);  //value 0   00000001
                flag.Set(0x02);  //value 0   00000011
                flag.Set(0x04);  //value 0   00000111
                flag.Set(0x08);  //value 0   00001111
     
            }
        }
    ...