A project previously required to upload a file to the document library in the sharepint site, which was processed in two ways:
1. directly use the main program code, but it must be placed on the Sharepoint Server for execution to succeed;
2. Using the Web service method, the main program can be stored in that PC at will to call and execute the upload action.
First, method 1 is successfully used. However, when method 2 is changed to method 2, the execution fails.
Cause and solution:
After a variety of checks and Internet access to Google, the final result is that there is a lack of settings, see the following red font code for spweb settings:
Web. allowunsafeupdates = true;
However, in the previous calls in the main program, this setting is not required and can be successful. Of course, the green part of the code is still not available, and spsite settings are as follows:
Site. allowunsafeupdates = true;
It seems that for the sake of security, you need to write both codes in the future. The code in the green part is sufficient to update the field value. However, this experience shows that sometimes the spsite-level allowunsafeupdates must be set to true.
The Code is as follows:
/// For actual use, input the specified SPS server URL/DOC lib URL/DOC name/DOC binary array
[Webmethod]
Public bool uploaddocument (string p_sserver_url,
String p_sdoclib_url,
String p_sdocname,
Byte [] p_docbinaryarray)
{
Bool result = false;
Spsite site = NULL;
Spweb web = NULL;
Try
{
Spsecurity. runwithelevatedprivileges (delegate ()
{
Site = new spsite (p_sserver_url );
Site. allowunsafeupdates = true;
Web = site. openweb ();
Web. allowunsafeupdates = true;
}
);
Spfolder sp_folder = web. getfolder (p_sdoclib_url );
If (sp_folder.exists)
{
Sp_folder.files.add (p_sdocname, p_docbinaryarray, true); // overwrite if exists
Sp_folder.update ();
Result = true;
}
Else
{
Result = false;
}
}
Catch (exception ex)
{
Result = false;
}
Finally
{
Site. Close ();
Site. Dispose ();
Web. Close ();
Web. Dispose ();
}
Return result;
}
/// For local testing, input the specified SPS server URL/DOC lib URL/DOC name/to open the local file and generate the doc binary array
// [Webmethod]
Public bool uploaddocument_localtest (string p_sserver_url,
String p_sdoclib_url,
String p_sdocname)
{
Bool result = false;
Spsite site = NULL;
Spweb web = NULL;
Try
{
Spsecurity. runwithelevatedprivileges (delegate ()
{
Site = new spsite (p_sserver_url );
Web = site. openweb ();
Site. allowunsafeupdates = true;
Web. allowunsafeupdates = true;
}
);
Spfolder sp_folder = web. getfolder (p_sdoclib_url );
If (sp_folder.exists)
{
Filestream FS = new filestream (@ "C:/domo.txt", filemode. Open );
Byte [] p_docbinaryarray = new byte [fs. Length];
FS. Read (p_docbinaryarray, 0, (INT) fs. Length );
Sp_folder.files.add ("demo.txt", p_docbinaryarray, true); // overwrite if exists
Sp_folder.update ();
Result = true;
}
Else
{
Result = false;
}
}
Catch (exception ex)
{
Result = false;
}
Finally
{
Site. Close ();
Site. Dispose ();
Web. Close ();
Web. Dispose ();
}
Return result;
}