Hi,

I doubt you have SPContext available in pages sitting in Layouts folder like 
DispForm which is causing the null reference error. You should be able to 
identify the exact problem by debugging your web part code. Attach the visual 
studio debugger to IIS and see what's going on.
 
Regards,
 
Shahram.


________________________________
 From: Web Admin <web.ad...@syd.catholic.edu.au>
To: ozMOSS <ozmoss@ozmoss.com> 
Sent: Wednesday, 17 October 2012 2:19 PM
Subject: Re: Hit counter for Announcement List Item
 
OK. I have decided to do this as a simple web part which can then be
added to a lists's DispForm as needed.

Everything builds and deploys but when trying to add the web part to
the form view I get:

"Unable to add web part(s): Object reference not set to an instance of
an object."

That's it. Nothing in Event viewer or ULS logs.

Pretty sure I've missed something obvious as this is my first wp from
scratch. Any assistance appreciated.

public class CounterWP : System.Web.UI.WebControls.WebParts.WebPart
{
        public bool thefield;
        public string counter;
        SPList theList;
        SPListItem theItem;

        public CounterWP()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.EnsureChildControls();

            // Get the list, current item and field collection
            theList = SPContext.Current.List;
            theItem = SPContext.Current.ListItem;
            SPFieldCollection fieldColl = theItem.Fields;

            if (fieldColl.ContainsField("Counter"))
            {
                counter = theItem["Counter"].ToString();
                int value;
                int.TryParse(counter, out value);
                // Increment the field
                counter = counter + 1;
                theItem.Update();

                thefield = true;
            }
            else
            {
                thefield = false;
            }

        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            try
            {
                // Counter field is present.
                if (thefield)
                {
                    this.Controls.Add(new LiteralControl("Hits: " + counter));
                }
                // No Counter field in list.
                else
                {
                    this.Controls.Add(new LiteralControl("<p
class=\"wp-content description\">This list does not contain a numeric
field named Counter.</p>"));
                }
                this.ChildControlsCreated = true;
            }
            catch (Exception ex)
            {
                this.Controls.Add(new LiteralControl("ERROR: " +
ex.ToString()));
            }
        }
    }
}

On 16 October 2012 14:54, Web Admin <web.ad...@syd.catholic.edu.au> wrote:
> Sorry that came out wrong. Wasn't show how to make it generic enough
> to target one or more lists.
>
> Should I use the additionalpageheader and pass in a param or two for
> list and item URL? Or maybe interrogate all lists that contain a
> particular field of "counter". Then use the query string to update the
> item's field by ID?
>
> Any tips appreciated. I'm sure this is eminently doable.
>
> On 16 October 2012 14:28, Web Admin <web.ad...@syd.catholic.edu.au> wrote:
>> Maybe I could modify the DispForm directly and push a function to
>> _spBodyOnLoadFunctionNames? This way I would capture all page loads
>> rather than just those accessed from the list item click event.
>>
>> A delegate control was my original thought but wasn't sure of the best
>> way to add it to the item display form.
>>
>> On 16 October 2012 14:19, Paul Turner <ptur...@smsmt.com> wrote:
>>> How about a delegate control?
>>>
>>>
>>>
>>>
>>>
>>> Regards
>>>
>>>
>>>
>>> Paul Turner
>>>
>>> Practice Lead - SharePoint
>>>
>>> SMS Management & Technology
>>>
>>>
>>> M 0412 748 168
>>>
>>> paul.tur...@smsmt.com
>>> www.smsmt.com
>>>
>>> About SMS: Level 29, Westpac House, 91 King William Street, Adelaide SA 5000
>>> SMS Management & Technology (SMS) [ASX:SMX] is Australia's largest publicly
>>> listed Management Services company. We solve complex problems and transform
>>> business through Consulting, Technology and Systems Integration
>>>
>>> P please consider the environment before printing this email
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf
>>> Of Ajay
>>> Sent: Tuesday, 16 October 2012 1:42 PM
>>> To: ozMOSS
>>> Subject: Re: Hit counter for Announcement List Item
>>>
>>>
>>>
>>> I am thinking a client side event for click event of viewing the list item,
>>> some javascript function calls web service, which updates the counter value
>>> somewhere.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> On Tue, Oct 16, 2012 at 3:17 PM, Web Admin <web.ad...@syd.catholic.edu.au>
>>> wrote:
>>>
>>> Hi Ivan,
>>>
>>> Yes, I mean to increment a field value (in same or other dedicated
>>> list) when the display form is loaded.
>>>
>>> I looked at the SPAudit class but it looks pretty scary and problematic.
>>>
>>> Wondering if I can use the OnLoad method within a custom control and
>>> add that via a web part to the form page?
>>>
>>> This is all beginning to sound a bit overwrought for a seemingly
>>> simply requirement. :\
>>>
>>> Cheers,
>>>
>>> Paul
>>>
>>>
>>> On 16 October 2012 12:55, Ivan Wilson <iv...@sharepointgurus.net> wrote:
>>>> Never tried, but possibly the audit logging feature. I assume you mean
>>>> with the item details page is displayed, rather than when the item is
>>>> included in a web part on a page?
>>>>
>>>> Another option could be to customize the view details page (possibly via
>>>> InfoPath) to include the functionality to increment. You will probably have
>>>> to deal with the situation where the current user doesn't have permissions
>>>> to update the announcement list. A work-around for that could be to store
>>>> the usage counts in a separate list that all staff have access to.
>>>>
>>>> Ivan
>>>>
>>>> -----Original Message-----
>>>> From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On
>>>> Behalf Of Web Admin
>>>> Sent: Tuesday, 16 October 2012 12:37 PM
>>>> To: ozMOSS
>>>> Subject: Hit counter for Announcement List Item
>>>>
>>>> Hi all,
>>>>
>>>> Wondering if it's possible to add an item event receiver that will
>>>> increment a numeric field value each time the item is viewed.
>>>>
>>>> The SPItemEventReceiver class doesn't seem to have a method for this.
>>>> Is there another way to achieve this?
>>>>
>>>> Regards,
>>>>
>>>> Paul
>>>> _______________________________________________
>>>> ozmoss mailing list
>>>> ozmoss@ozmoss.com
>>>> http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
>>>>
>>>>
>>>> _______________________________________________
>>>> ozmoss mailing list
>>>> ozmoss@ozmoss.com
>>>> http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
>>>
>>>
>>> --
>>> Regards,
>>>
>>> Paul Noone
>>>
>>> SharePoint Farm Admin/Developer
>>> Infrastructure Team
>>> CEO Sydney
>>>
>>> p: (02) 9568 8461
>>> f: (02) 9568 8483
>>> e: paul.no...@syd.catholic.edu.au
>>> w: http://www.ceosyd.catholic.edu.au/
>>>
>>> _______________________________________________
>>> ozmoss mailing list
>>> ozmoss@ozmoss.com
>>> http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
>>>
>>>
>>>
>>>
>>> ________________________________
>>> NOTICE - This communication is intended only for the person or entity to
>>> which it is addressed and may contain confidential and/or privileged
>>> material. Any review, retransmission, dissemination or other use of, or
>>> taking any action in reliance on, this communication by persons or entities
>>> other than the intended recipient is prohibited. If you are not the intended
>>> recipient of this communication please delete and destroy all copies and
>>> telephone SMS Management & Technology on 1300 842 767 immediately. Any views
>>> expressed in this Communication are those of the individual sender, except
>>> where the sender specifically states them to be the views of SMS Management
>>> & Technology. Except as required by law, SMS Management & Technology does
>>> not represent, warrant and/or guarantee that the integrity of this
>>> communication has been maintained nor that the communication is free from
>>> errors, virus, interception or interference.
>>>
>>> _______________________________________________
>>> ozmoss mailing list
>>> ozmoss@ozmoss.com
>>> http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
>>>
>>
>>
>>
>> --
>> Regards,
>>
>> Paul Noone
>>
>> SharePoint Farm Admin/Developer
>> Infrastructure Team
>> CEO Sydney
>>
>> p: (02) 9568 8461
>> f: (02) 9568 8483
>> e: paul.no...@syd.catholic.edu.au
>> w: http://www.ceosyd.catholic.edu.au/
>
>
>
> --
> Regards,
>
> Paul Noone
>
> SharePoint Farm Admin/Developer
> Infrastructure Team
> CEO Sydney
>
> p: (02) 9568 8461
> f: (02) 9568 8483
> e: paul.no...@syd.catholic.edu.au
> w: http://www.ceosyd.catholic.edu.au/



-- 
Regards,

Paul Noone

SharePoint Farm Admin/Developer
Infrastructure Team
CEO Sydney

p: (02) 9568 8461
f: (02) 9568 8483
e: paul.no...@syd.catholic.edu.au
w: http://www.ceosyd.catholic.edu.au/
_______________________________________________
ozmoss mailing list
ozmoss@ozmoss.com
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
_______________________________________________
ozmoss mailing list
ozmoss@ozmoss.com
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss

Reply via email to