none
error "object reference not set to an instance of an object" when open custom webpart in sharepoint Designer

    Question

  • Hi All

    I have created custom webparts and use these webparts into Moss site and it is working fine, but when we try to open the page(which contains custom webparts), into Sharepoint Designer then error "object reference not set to an instance of an object" is displayed at the Webparts and Webparts are not able to visible in Sharepoint Designer.

    Please Suggest the solution.

    Thanks
    Ashish
    • Moved by Mike Walsh FIN Thursday, July 02, 2009 2:55 PM spd q (From:SharePoint - Development and Programming)
    Thursday, July 02, 2009 2:35 PM

All replies

  • spd questions go to Design/customization

    Moving this


    WSS FAQ sites: http://wssv2faq.mindsharp.com and http://wssv3faq.mindsharp.com
    Total list of WSS 3.0 / MOSS 2007 Books (including foreign language) http://wssv3faq.mindsharp.com/Lists/v3%20WSS%20FAQ/V%20Books.aspx
    Thursday, July 02, 2009 2:55 PM
  • I´m not sure, but if you use the context maybe on designer context be null and because of it, give you this error.

    What kind of webpart or operations do? Can you put some code?
    Microsoft MCP 6608628
    Thursday, July 02, 2009 3:11 PM
  • We have created custom webpart which is inherited from Microsoft System.web.ui.webcontrols.webparts.Webpart class.
    like this

    public class CustomWebPart01 : WebPart
    {
    implementation code
    }

     I have also remove the use of SPcontext to pick site url and start picking site URL from web.config fileas using appsettings, but still the same error has woccurs.

    Friday, July 03, 2009 11:28 AM
  • Can you edit an out of the box webpart with SPD?  Just trying to find out if its a code issue with your custom webpart or a SPD issue?  If you can edit a standard webpart, can you post your code for your custom webparts?

    Dave
    My SharePoint Blog - http://www.davehunter.co.uk/blog
    Friday, July 03, 2009 2:16 PM
  • Hi

    I have tried a lot and come to know that when I put my custom web part dll into bin directory of the the web site and then add custom web part to the web page of the site and open the page (containing custom web part) in sharepoint designer is displaying fine.
    but when i put same dll into GAC(C:\windows\assembly) and add custom web part to the web page of the site and open it in the sharepoint designer then error "object reference not set to an instance of an object" occurs.
    Means the same dll is working fine from bin directory of the site.

    But the requirement is to put the dll in the GAC(C:\windows\assembly), so please suggest the solution.
    code for the custom web part dll is as follows

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using System.Web;
    using System.Net;
    namespace Test
    {
        public class CustomWebPart01 : WebPart
        {
            private TeamwareServiceInfo twSvcInfo;
            private string _strTwSrvUrl = "";
            //Default Constructor
            public CustomWebPart01()
            {
                Settings twSettings = (Settings)HttpContext.Current.Application[Def.TeamwareSettings];
                twSvcInfo = twSettings.DefaultTwSvcInfo;
                _strTwSrvUrl = twSvcInfo.ToString();

            }
            private string _strDefaultUrl = "";
            private string _strWidth = "100";
            private string _strHeight = "100";

            //Custom property to display URL in property sheet.
            [WebBrowsable(true),
            WebDescription("Displays a custom message"),
            WebDisplayName("To specify a link, type a URL or path."),
            Personalizable(PersonalizationScope.User)]

            public string DisplayMessage
            {
                get
                {
                    return _strDefaultUrl;
                }
                set
                {
                    _strDefaultUrl = value;
                }
            }

            //Custom property for width in the property sheet.
            [WebBrowsable(true),
            WebDescription("Display IFrame width in %"),
            WebDisplayName("Enter IFrame Width[%] "),
            Personalizable(PersonalizationScope.User)]

            public string FrameWidth
            {
                get
                {
                    return _strWidth;
                }
                set
                {
                    _strWidth = value;
                }
            }

            //Custom property to display height in the property sheet.
            [WebBrowsable(true),
            WebDescription("Display IFrame Height in %"),
            WebDisplayName("Enter IFrame Height[%] "),
            Personalizable(PersonalizationScope.User)]

            public string FrameHeight
            {
                get
                {
                    return _strHeight;
                }
                set
                {
                    _strHeight = value;
                }
            }

            //overriding the systems default CreateChildControls method
            protected override void CreateChildControls()
            {
                string _strRequestUrl = "";

                //Code for picking default setting
                if ((_strDefaultUrl == "") && (_strTwSrvUrl != ""))
                {
                    _strDefaultUrl = _strTwSrvUrl;
                }
                else if (_strTwSrvUrl != "")
                {
                    int nPos = _strDefaultUrl.IndexOf("//");
                    string _strTemp = "";
                    int nPos1 = -1;
                    if (nPos != -1)
                    {
                        nPos1 = _strDefaultUrl.IndexOf('/', nPos + 2);
                        if (nPos1 != -1)
                        {
                            _strTemp = _strDefaultUrl.Substring(nPos1 + 1);
                        }
                    }
                    _strDefaultUrl = _strTwSrvUrl + _strTemp;
                }
                if (_strDefaultUrl != "")
                {
                    int nPos = _strDefaultUrl.IndexOf("//");
                    int nPos1 = -1;
                    if (nPos != -1)
                    {
                        nPos1 = _strDefaultUrl.IndexOf('/', nPos + 2);
                        if (nPos1 != -1)
                        {
                            _strRequestUrl = _strDefaultUrl.Substring(0, nPos1);
                        }
                        else
                        {
                            _strRequestUrl = _strDefaultUrl;
                        }
                    }
                }
                base.CreateChildControls();
                HtmlGenericControl twoFrame = new HtmlGenericControl("iframe");
                twoFrame.ID = "IEFrame";

                //Code to check that temaware server is running or not
                //Commented code is for proxy settings.
                try
                {
                    HttpWebResponse _httpWbResponse = null;
                    HttpWebRequest _httpWbRequest = null;
                    if (_strRequestUrl != "")
                    {
                        _httpWbRequest = (HttpWebRequest)WebRequest.Create(_strRequestUrl);
                        _httpWbRequest.ServicePoint.ConnectionLimit = 50;
                        _httpWbRequest.Timeout = 200000;
                        _httpWbResponse = (HttpWebResponse)_httpWbRequest.GetResponse();
                        if ((_httpWbResponse.StatusCode == HttpStatusCode.OK))
                        {
                            twoFrame.Attributes.Add("src", _strDefaultUrl);
                        }
                    }

                }
                catch (Exception)
                {
                    twoFrame.Attributes.Add("src", "/_LAYOUTS/ErrorMessages.aspx");
                }
                //End
                twoFrame.Attributes.Add("width", _strWidth + "%");
                twoFrame.Attributes.Add("height", _strHeight + "%");
                twoFrame.Attributes.Add("frameborder", "0");
                twoFrame.Attributes.Add("border", "0");
                this.Controls.Add(twoFrame);
            }
           
        }
    }


    Thanks
    Ashish

    Wednesday, July 08, 2009 1:47 PM
  • I think I found an answer for this question. Your CreateChildControls method will run, but the runtime objects are not present.

    Cheers
    Jan Steinar Kvilesjo

    SharePoint Designer 2007 is a ground breaking design tool to help information workers and designers create context rich composite applications without writing single line of code. SharePoint Designer enables many interesting customization and app building scenarios that might not have been originally envisioned by the control developers. Moreover the control preview and persistence mechanism is built on top of the Whidbey control designer framework which is inherently different from the ASP.Net runtime behaviors. That’s the reason sometimes control developers might be surprised to find out controls which work perfectly in browsers might not work at all in SharePoint Designers. Most of the time to fix the issue we need to tweak or redesign the server control in a way that is more compatible with the Whidbey and SharePoint design time framework.

    This document tries to summarize some of the best practice of design time friendly server controls to help control developers avoid most common bugs when controls interact with SharePoint Designer. Actually all best practices listed below work well with Visual Studio design surface too so by following the practice we can potentially have the benefit of making our controls work better with VS design areas.

    1: Just because a control works in browser doesn’t mean it’ll automatically work in SPD, and usually it’s not SPD or designer framework bug.

    SharePoint Designer leverages Whidbey ControlDesigner framework to let WSS server renders control preview for SPD. This process is inherently different from runtime control rendering in many ways:

    · When a control is being rendered for design time, the Page context is different from runtime. Many global variables that used to work in runtime doesn’t exist or even will throw uncaught exceptions if we’re not careful. Page.Request is one prime example.

    · Control lifecycle is different. Default control designer framework doesn’t call OnPreRender and OnLoad. Only OnInit and Render are guaranteed to be called.

    · Control tree structure might be different. Many times controls are added to designer Tree structure in a “flatted out” way, instead of the runtime hierarchical structure.

    · In SPD users can update control property in property grid of SPD. This involves SPD sending a remote authoring command to SharePoint server, instantiate the control at server, setting the property, and then gather the markup out of the in memory control at server, and finally sending the markup and preview back to SPD client. This process involves “roundtrip” between markup and in-memory control, unlike runtime ASP.Net parsing which is only one-directional. Because of this complication, the control and its markup requires more from control developers in order for this roundtrip to work seamlessly.

    · SPD users can easily drag & drop controls around design surface.

    · SPD 2007 can’t run JavaScript code or any other client side code as in browsers. Future releases of SPD might revisit this issue but there’s no guarantee it’ll work next release.

    That’s the reason it’s highly recommended that developers verify server control preview fine in SPD and we can update control property through SPD property grid before check in.

    • Proposed as answer by kvile Tuesday, January 05, 2010 10:49 AM
    Tuesday, January 05, 2010 10:27 AM