Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 557311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:00:47+00:00 2026-05-13T12:00:47+00:00

Using the following block of code, the listItem.Update fails with a NullReferenceException: SPWeb web

  • 0

Using the following block of code, the listItem.Update fails with a NullReferenceException:

        SPWeb web = null;
        SPList list = null;
        SPListItem listItem = null;
        try
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(this.SiteUrl))
                {
                    web = site.OpenWeb();
                    list = web.Lists[this.ListName];
                    listItem = list.Items.Add();
                    listItem["Background"] = "foo";

                }
            }
            );
            listItem.Update();
        }
        catch
        {
        }
        finally
        {
            web.Dispose();
        }

If I move the listItem.Update() method inside of the anonymous delegate, I get “Operation is not valid due to the current state of the object.”

Yes, I’ve combed through SO and have tried many permutations without success.

Any ideas?

Update:
After the first comment, I tried to remove the anonymous delegate from the code to see if it fared any better:

    // store the selected item to pass between methods
    public T SelectedItem { get; set; }   

    // set the selected item and call the delegate method
    public virtual void Save(T item)
    {
        SelectedItem = item;
        try
        {
            SPSecurity.RunWithElevatedPrivileges(SaveSelectedItem);
        }
        catch
        {
        }
    }

    public virtual void SaveSelectedItem()
    {
        if (SelectedItem != null)
        {

            using (SPSite site = new SPSite(this.SiteUrl))
            {
                using(SPWeb web = site.OpenWeb())
                {                    
                    SPList list = web.Lists[this.ListName];
                    SPListItem listItem = list.Items.Add();
                    //UpdateListItem(listItem, SelectedItem);
                    listItem["Background"] = "foo";
                    listItem.Update();
                }
            }      
        }
    }

And this still fails “Operation is not valid due to the current state of the object.” In both code samples, it looks like site.Impersonating is false. I am using Windows Auth, and Impersonation in the web.config. This is running from the ASP.Net Development server.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:00:47+00:00Added an answer on May 13, 2026 at 12:00 pm

    I found an example from this site (blackninjasoftware). I create a reference to the site, grab its SystemAccount token and then create another reference to the site, using the admin token. It seemed a little hackish to me at first – but hey – I have a deadline.

    Final working method body now looks like:

                SPListItem new_item = null;
                SPSite initialSite = new SPSite(this.SiteUrl);
                using (var site = new SPSite(this.SiteUrl, initialSite.SystemAccount.UserToken))
                {
                    // This code runs under the security context of the SHAREPOINT\system
                    // for all objects accessed through the "site" reference. Note that it's a 
                    // different reference than SPContext.Current.Site. 
                    using (var elevatedWeb = site.OpenWeb())
                    {
                        elevatedWeb.AllowUnsafeUpdates = true;
                        SPList list = elevatedWeb.Lists[this.ListName];
                        new_item = list.Items.Add();
                        UpdateListItem(new_item, item);
                        if (new_item != null)
                        {
                            new_item.Update();
                        }
                    }
                }
                initialSite.Dispose();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 347k
  • Answers 347k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you may be working too hard, and you… May 14, 2026 at 6:21 am
  • Editorial Team
    Editorial Team added an answer Code 128 can do 128 ASCII characters so set the… May 14, 2026 at 6:21 am
  • Editorial Team
    Editorial Team added an answer UNPIVOT may work for you, assuming your input table MyTable… May 14, 2026 at 6:21 am

Related Questions

I'm trying to build the following block of code in a 1-file .cpp file:
When you create a new MFC application, the wizard creates the following block of
I'm experiencing an error when using the windows installer to install an event source
I'm using PDFBox for a C# .NET project. and I'm getting a TypeInitializationException (The
I'm reading from a binary data file which is written by invoking the following

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.