Use Web service to upload files to the SPS Document Library: spweb. allowunsafeupdates = true

Source: Internet
Author: User

 

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;
}

 

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.