Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have the following CEO message Web part which has the code as follows. When I edit the web part it is always shows as hidden Ticked even though it is un-ticked and saved. I can see this web part in the web part gallery. Please suggest me the best way to fix this issue.

private void LoadCEOMEssageDetails()
    {
        // Get CEO message new item
        NewsItem message = null;
        NewsHelper helper = new NewsHelper();
        //using (SPSite site = new SPSite(NewsSiteUrl))
        using (SPSite site = SPContext.Current.Web.Site)
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList list = web.Lists.TryGetList(NewsListTitle);
                if (list != null)
                    message = helper.GetCEOMessage(list);
            }
        }

        if (message == null)
        {
            (this.Parent as WebPart).Hidden = true;
            return;
        }

        // Set the 'new' icon
        if (message.IsNewItem)
        {
            imgNewMsg.Visible = true;
            imgNewMsg.ImageUrl = "/_layouts/1033/images/new.gif";
        }

        // Display the details
        lblCeoMessageTitle.Text = message.Title;
        lblCeoMessageTitle.NavigateUrl = message.MoreUrl;
        lblMessageDate.Text = message.StartDateString;
        lblCeoMessage.Text = message.Headline;
        imgCeoImageUrl.ImageUrl = message.ThumbnailUrl;
        hlReadMore.NavigateUrl = message.MoreUrl;
        hlViewMore.NavigateUrl = string.Format("{0}{1}?ct={2}", NewsSiteUrl, PageConstants.NewsTimelinePage_Url, message.ContentType);
    }
share|improve this question
add comment

1 Answer 1

My bet is that the problem lies with this code as it is probably being run at a time when, for whatever reason, the message is null. This could be due to edit mode but is more likely that there is a bug in the helper module. What event calls this?

if (message == null)
{
    (this.Parent as WebPart).Hidden = true;
    return;
}

However, I do have to ask, why not merely use a Content Query Web Part for this as that is what it is designed to do?

One other point - you should not use using{} blocks when referencing the context web or context site. Simply reference it as needed but make sure that you don't call a Dispose() on it since it is not an object you created. In fact, the way that you are referencing it could actually cause greater problems because you are disposing of an object that you didn't create thus making it unavailable/unstable for any downstream callers.

do this instead and get rid of the using{} :

        SPList list = SPContext.Current.Web.Lists.TryGetList(NewsListTitle);
        if (list != null)
            message = helper.GetCEOMessage(list);
share|improve this answer
1  
+1 for the using{} clarification, but also, my question is, where does NewsListTitle come from? If that is causing TryGetList() to fail, which in turn would cause list to be null, then message will never be, um, gotten, and would therefore always be null... –  Dylan Cristy Mar 28 '13 at 16:04
    
Just now, I have checked the web part property. It is having the status as "missing safecontrols entry". Actually, I have added the entry to web.config. But still it is not showing the webpart. –  Ram Mar 28 '13 at 16:27
    
Please clarify: is your problem that you are missing the SafeControls entry or that the web part is not appearing? These are two considerably different issues. –  Dave Wise Mar 28 '13 at 16:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.