Tuesday, August 30, 2011

Sandbox WebPart

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SandboxedWebPart.WebPart1
{
    [ToolboxItemAttribute(false)]
    public class WebPart1 : WebPart
    {
        protected override void CreateChildControls()
        {
            Label message1 = new Label();
            Controls.Add(message1);

            Controls.Add(new WebControl(HtmlTextWriterTag.Br));
            Controls.Add(new WebControl(HtmlTextWriterTag.Br));
            Button testButton1 = new Button();
            testButton1.Text = "Test 1";
            testButton1.Click += delegate
            {
                message1.Text = string.Format("Site having {0} Lists", SPContext.Current.Web.Lists.Count);
            };
            Controls.Add(testButton1);

            Button testButton2 = new Button();
            testButton2.Text = "Test 2";
            testButton2.Click+=delegate
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(
                        delegate
                        {
                            using (SPSite siteCollection = new SPSite(SPContext.Current.Site.ID))
                            {
                                SPWeb web = siteCollection.OpenWeb();
                                message1.Text = string.Format("Site having {0} Lists", web.Lists.Count);
                            }
                        });
                }
                catch (Exception e)
                {
                    message1.Text = e.Message;
                }
            };
            Controls.Add(testButton2);

            Button testButton3 = new Button();
            testButton3.Text = "Test 3";
            testButton3.Click += delegate
            {
                try
                {
                    System.Net.HttpWebRequest.Create("http://crm-server:30312");
                }
                catch (Exception e)
                {
                    message1.Text = e.Message;
                }
            };
            Controls.Add(testButton3);

            Button testButton4 = new Button();
            testButton4.Text = "Test 4";
            testButton4.Click += delegate
            {
                try {
                    SPWeb site = SPContext.Current.Web;
                    SPSiteDataQuery query = new SPSiteDataQuery();
                    query.Lists = @"<Lists ServerTemplate=""104""/>";
                    query.Webs = @"<Webs Scope=""Recursive""/>";
                    message1.Text="No.Of Site Records "+site.GetSiteData(query).Rows.Count;
                }
                catch(Exception e)
                {
                    message1.Text = e.Message;
                }
            };
            Controls.Add(testButton4);
        }
    }
}

No comments:

Post a Comment