Go to splist update

Source: Internet
Author: User

From: http://www.cnblogs.com/MinSentinel/archive/2008/06/07/1209992.html

 

Through splist. update () can update a list, but generally you cannot directly call this method, and "updates are currently disallowed on GET requests. to allow updates on a get, set the 'allowunsafeupdates' property on spweb."

 

There are two solutions:
1. Set allowunsafeupdates = true. This is a very clever method. It is not only available here, but can be avoided if you set this value when the above errors are caused by other reasons. I have seen that spweb is required for some projects. But it has security risks. For details, refer to msdn.

 

2. Using POST requests instead of GET requests can also be effectively solved by using runwithelevatedprivileges. Do not use GET requests instead of post requests. Simply put, you should not update the list directly in events such as page_load. You can put a button and use it to trigger the event. Then, run the update code in runwithelevatedprivileges.
String url = spcontext. Current. Site. url;
Spsecurity. runwithelevatedprivileges (delegate ()
{
Using (spsite site = new spsite (URL ))
{
Using (spweb web = site. openweb ())
{
Splist list = web. Lists [listname];
List. Update ();
}
}
}
);

Generally, this can meet your needs.

==========================================

 

[Problem description]

When updating list items in the list, we often need to use spsecurity. runwithelevatedprivileges (delegate () {} for item. Update (), but sometimes this will cause errors.

 

Solution 1]

1. Put item. Update () after permission escalation, that is, only the site and text are placed in the elevation permission:

Splist list = NULL;

Spsecurity. runwithelevatedprivileges (delegate ()

{

Site = new spsite (sitecollectionurl );

Web = site. rootweb;

});

 

List = web. Lists ["Notification library"];

Splistitem item = List. Items. Add ();

IItem [title] = "123 ";

Item. Update ();

 

2. After the permission is elevated, similar errors may occur when you operate the listitem operation. You can solve the problem in the same way:

Protected void page_load (Object sender, eventargs E)

{

 

Spsecurity. runwithelevatedprivileges (delegate ()

{

Updateitem ();

}

);

}

 

Void updateitem ()

{

Spsite site = new spsite ("http: // ssjin073: 9032 ");

 

Spweb web = site. rootweb;

 

// Force set the context object

Httpcontext. Current. items ["httphandlerspweb"] = web;

 

Web. allowunsafeupdates = true;

Splist list = web. Lists ["mainlist"];

 

Splistitem item = List. items [0];

 

Item ["title"] = datetime. Now. tostring ();

 

Item. Update ();

 

Web. Dispose ();

Site. Dispose ();

}

 
The second method comes from: http://www.cnblogs.com/jianyi0115/archive/2008/04/11/1148827.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.