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.