Question

I'm new at developing an web part with Visual Studio. I want a weather web part. I created an empty SP 2013 Project, added Web Part item. But when I deploy it and add the custom webpart into my site. It gives an error:

Sorry, something went wrong An unexpected error has occurred.

Web Parts Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove personal settings. For more information, contact your site administrator.

I deleted the webpart and add again. Nothing changed. What else should I have to do? Thank you.

My WebPart item name : HavaDurumuWP My Project name: HavaDurumu

Elements.xml:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
  <Module Name="HavaDurumuWP" List="113" Url="_catalogs/wp">
    <File Path="HavaDurumuWP\HavaDurumuWP.webpart" Url="HavaDurumuWP_HavaDurumuWP.webpart" Type="GhostableInLibrary">
      <Property Name="Group" Value="Custom" />
    </File>
  </Module>
</Elements>

Here is my HavaDurumuWP.cs:

namespace HavaDurumuWP.HavaDurumuWP
{
    [ToolboxItemAttribute(false)]
    public class HavaDurumuWP : WebPart
    {

        public void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (!e.Cancelled && e.Error == null)
            {
                string res = Convert.ToString(e.Result);
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(res);
                var table = doc.DocumentNode.SelectSingleNode("//table[@class='tbl_sond']");
                var degree = table.SelectSingleNode("//td[@class='renkMax']");
                var date = table.SelectSingleNode("//td[@class='sond_zaman']");

            }
            LiteralControl filterMessage = new LiteralControl("ece");

            this.Controls.Add(filterMessage);

        }
        protected override void CreateChildControls()
        {
            string url = "http://www.mgm.gov.tr/tahmin/il-ve-ilceler.aspx?m=ISTANBUL";

            WebClient webclient = new WebClient();
            webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

            Uri uri = new Uri(url);
            webclient.DownloadStringAsync(uri);
        }
    }
}
Was it helpful?

Solution

It seems like somewhere inside your webpart an exception is thrown. I would suggest you to wrap all your code inside CreateChildControls and client_DownloadStringCompleted methods with try..catch something like that:

try{
    ...
} catch (Exception ex){
    this.Controls.Add(new LiteralControl(ex.Message+"; "+ex.StackTrace));
}

It is a good practice to handle all possible exceptions inside webpart to disalow them to float up to the page (in the opposite case this exception can break the whole page).

Also you can try to debug your webpart (F5 in Visual Studio) to identify the place where exception is thrown.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top