Microsoft Technology, .Net, BizTalk, Sharepoint & etc.

Liedong(Ken) Zheng, Senior SharePoint Developer at SIMPLOT

Posts Tagged ‘Web Part’

Random Image Web Part

Posted by ken zheng on October 22, 2008

Here is the simple custom webpart which random displays the image from picture library.

You can edit the web part by change the URL and picture library name
Random Image Web Part

public class RandomImage : System.Web.UI.WebControls.WebParts.WebPart
    {
        string _SiteURL = SPContext.Current.Web.Url;
        string _PicLibName = "Pic Lib";

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        FriendlyName("The Site URL of Picture Library")]
        public string SiteURL
        {
            get
            {
                return _SiteURL;
            }
            set
            {
                _SiteURL = value;
            }
        }

        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        FriendlyName("The Name of Picture Library")]
        public string PicLibName
        {
            get
            {
                return _PicLibName;
            }
            set
            {
                _PicLibName = value;
            }
        }

        Image myimage = new Image();
        protected override void CreateChildControls()
        {
            myimage.Height = 256;
            myimage.Width = 256;
            try
            {
                using (SPSite mysite = new SPSite(SiteURL))
                {
                    //mysite = SPControl.GetContextSite(Context);

                    using (SPWeb myweb = mysite.OpenWeb())
                    {
                        SPList mylist = myweb.Lists[PicLibName];
                        SPQuery myquery = new SPQuery();
                        myquery.Query = "";

                        string serverpath = mysite.Url.ToString() + "/";
                        SPListItemCollection mylistitem = mylist.GetItems(myquery);
                        if (mylistitem.Count > 0)
                        {
                            System.Random RandNum = new System.Random();
                            int MyRandomNumber = RandNum.Next(mylistitem.Count);
                            myimage.ImageUrl = serverpath + mylistitem[MyRandomNumber].Url.ToString();
                        }
                        else
                        {
                            this.Page.Response.Write("No image found");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Controls.Clear();
                Label errorMessage = new Label();
                errorMessage.Text = "There was an error in the code.  Please contact your system administrator and rely the following " +
                    "message: " + ex.Message;
                Controls.Add(errorMessage);
            }

            base.CreateChildControls();
        }
        protected override void Render(HtmlTextWriter writer)
        {
            myimage.RenderControl(writer);
            // TODO: add custom rendering code here.
            // writer.Write("Output HTML");
        }
    }

Posted in Sharepoint | Tagged: | 2 Comments »

Easily get to the WebPart Maintenance page for any Sharepoint Site Url

Posted by ken zheng on September 29, 2008

Symptom:

Often times, developers in WSS3.0/MOSS2007 probably run across the issue where a web part is dropped on to a page
and then Sharepoint errors out for one reason or the other. Sometimes, the user is directed to a Web Part Maintenance page,
which helps the developer remove the offending web part from the page.

However, there is no way to get to this page from any easily available link.

Resolution:

In order to get to the Web Part Maintenance page for ANY sharepoint url, try the following trick.

Append the querystring ?contents=1 to the url in the browser as shown below
http://portal/area/default.aspx?contents=1

Posted in Sharepoint | Tagged: , , | Leave a Comment »