Enhance SharePoint code Permissions

Source: Internet
Author: User

I was not aware of this before when I wrote a domain-based SharePoint site.CodeBecause I log on as an administrator. After the website authentication is changed to Forms authentication, the general user is not the website administrator, and some controls may be rejected. For example, if an Internet user wants to write data to a document library, the access is denied.

Public   Void Writetolib ()
{
Byte [] File = .....; // Get byte array
Spsite site =   New Spsite ( " URL " );
Spweb = Site. openweb ( " URL " );
Spfolder lib = Web. folders [ " Libname " ];
Spfilecollection files = Lib. files;
Files. Add ( " Filename " , File ); // Access denied
}

The same Code does not have this permission if the user is a website administrator.
So what is the solution? We need to improve the permissions of this Code, regardless of whether the current user has sufficient permissions. From the SharePoint SDK, we can see that:

Spsecurity. codetorunelevated elevatedwritetolibrary =   New Spsecurity. codetorunelevated (writetolib );
Spsecurity. runwithelevatedprivileges (elevatedwritetolibrary );

In this way, the permission of our method is raised to the system account level, and the problem is solved.
If the spsite object is used, it must be created inside the method. spcontext. Current. site cannot be used, otherwise it will not work.

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.