Tuesday, November 8, 2011

Add Custom Property Panel in Custom WebPart like OOB SharePoint Property Panel via Reflection


Sometime we have a requirement to create custom property in WebPart. But the custom Property will add in “Miscellaneous” section or added in blank section if we add property via custom tool part. Now if you want to create panel like OOB Panel “Layout, Appearance etc.” then use below method.
Below class is static class for enabling extension method on ToolPart class. You need to add below class in your solution and add namespace in your toolpart class where you want to use this method
namespace ExtensionMethods
{
    public static class CustomExtensions
    {
        public static Panel GetPropertyPanel(this ToolPart currentToolPart, Table table, String sTitle)
        {
            Panel controlPanel = new Panel();
            controlPanel.ID = "propertyPanelHideDisplay";
            controlPanel.Attributes.Add("id", currentToolPart.ClientID + "_" + controlPanel.ID);
            controlPanel.Controls.Add(table);

            Literal lt = new Literal();
            String sScript = "<script language='javascript'>\n" +
                            " var objDiv = document.getElementById('" + currentToolPart.ClientID + "_" + controlPanel.ID + "');\n" +
                            " objDiv.parentNode.parentNode.parentNode.attributes.removeNamedItem('colspan');\n" +
                            " objDiv.parentNode.parentNode.parentNode.attributes.removeNamedItem('class'); \n" +
                            "</script>";
            lt.Text = sScript;
            controlPanel.Controls.Add(lt);

            Type type = typeof(SPSite);
            Assembly assembly = type.Assembly;

            var bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

            Panel propertyPanel = (Panel)assembly.CreateInstance("Microsoft.SharePoint.WebPartPages.TPPanel", false, bindingFlags, null,
               new object[] { sTitle, controlPanel, true }, null, null);

            return propertyPanel;
        }
  
 }
}

Below is the sample class for ToolPart for adding Panel by using above class

using ExtensionMethods;
namespace CustomNameSpace
{
    class CustomToolpart : ToolPart
    {
        private DropDownList ddlDisplayType;
     
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            CreateControls();
        }

        public override void ApplyChanges()
        {
            EnsureChildControls();
            SendDataToWebPart();
        }

        private void SendDataToWebPart()
        {
            EnsureChildControls();
            CustomWebPart customWebPart = (CustomWebPart)this.ParentToolPane.SelectedWebPart;

            // Send the custom text to the Web Part.
            if (ddlDisplayType != null)
            {
                customWebPart.Property = ddlDisplayType.SelectedValue;
            }

           
        }

        public override void SyncChanges()
        {
            base.SyncChanges();
           
        }

        private void SetValues()
        {
            ListItem item = null;
            CustomWebPart customWebPart = (CustomWebPart)this.ParentToolPane.SelectedWebPart;
            if (!string.IsNullOrEmpty(customWebPart.Property))
            {
                item = ddlDisplayType.Items.FindByValue(customWebPart.Property);
                if (item != null)
                {
                    ddlDisplayType.SelectedValue = item.Value;
                }
            }
        
           
        }

        public override void CancelChanges()
        {
            base.CancelChanges();
        }


        protected override void RenderToolPart(System.Web.UI.HtmlTextWriter output)
        {
            base.RenderToolPart(output);
        }


        public void CreateControls()
        {
          
            ddlDisplayType = new DropDownList();

            ddlDisplayType.Items.Add("Property 1");
            ddlDisplayType.Items.Add("Property 2");
            ddlDisplayType.Items.Add("Property 3");

            AddControls();
            SetValues();
          
        }

        private void AddControls()
        {
            Table table = new Table();
            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            Literal ltStatic = new Literal();

            ltStatic.Text = "Custom Property";
            td.Controls.Add(ltStatic);
            tr.Cells.Add(td);
            table.Rows.Add(tr);
           
            tr = new TableRow();
            td = new TableCell();
            td.Controls.Add(ddlDisplayType);
            tr.Cells.Add(td);
            table.Rows.Add(tr);

            String sTitle = "Panel Title";
            this.Controls.Add(this.GetPropertyPanel(table, sTitle));
        }
    }
}


In above class bold line will return Panel and add the Panel in ToolPart Pane. Below is the screen shot for the above implementation


Hope it help !!!!

Monday, October 17, 2011

Display Unsupported Browser Error via OOB control in SharePoint

SharePoint 2010 will support many major browsers including IE7, IE8, FF3.5 and Safari4, but there is one browser that will not be supported by the out of the box SharePoint 2010 experience: Internet Explorer 6



How can we handle this gracefully? Microsoft again comes to the rescue with a SharePoint Control to help.
<SharePoint:WarnOnUnsupportedBrowsers runat="server"/>
If you place this control at the bottom of your master page, IE6 users will be greeted with a message like this:
image


Happy Coding !!!!!

Tuesday, October 11, 2011

SPChangeQuery Class in SharePoint

Gets the changes to the list from the change log as filtered by the specified query.



using (SPSite siteCollection = new SPSite("http://localhost"))
         {
            using (SPWeb webSite = siteCollection.OpenWeb())
            {
               // Get a list.
               SPList list = webSite.Lists[0];

               // Construct a query.
               SPChangeQuery query = new SPChangeQuery(false,  // limit object types
                                                       false); // limit change types

               // Specify the object type. 
               query.Item = true;

               // Specify change types. 
               query.Add = true;
               query.Delete = true;
               query.Update = true;

               SPTimeZone timeZone = webSite.RegionalSettings.TimeZone;
               int total = 0;

               // Loop until we reach the end of the log.
               while (true)
               {
                  SPChangeCollection changes = list.GetChanges(query);

                  total += changes.Count;

                  // Print info about each change to the console.
                  foreach (SPChangeItem change in changes)
                  {
                     // Get the item name.
                     string itemName = String.Empty;
                     SPListItem item = null;
                     try
                     {
                        item = list.GetItemByUniqueId(change.UniqueId);
                        itemName = item.Name;
                     }
                     catch (ArgumentException)
                     {
                        itemName = "Unknown";
                     }

                     Console.WriteLine("\nDate: {0}",
                         timeZone.UTCToLocalTime(change.Time).ToString());
                     Console.WriteLine("Change: {0}", change.ChangeType);
                     Console.WriteLine("Item: {0}", itemName);

                  }

                  // Break out of loop if we have the last batch.
                  if (changes.Count < query.FetchLimit)
                     break;

                  // Otherwise, go get another batch.
                  query.ChangeTokenStart = changes.LastChangeToken;
               }

               Console.WriteLine("\nTotal of {0} changes to {1} list", total, list.Title);
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();