SharePoint 2010 – Upload di un file su SharePoint 2010 Foundation

Upload di un file all’interno di una Document Library di SharePoint 2010 Foundation, senza la necessità di installare SharePoint


Per il caricamento è necesario avere le credenziali di un utente che abbia i permessi di “Add Item” sulla Library sulla quale si vuole effettuare il caricamento.

pre.CICodeFormatter{ font-family:arial; font-size:12px; border:1px dashed #CCCCCC; width:99%; height:auto; overflow:auto; background:#f0f0f0; line-height:20px; background-image:URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); padding:0px; color:#000000; text-align:left; } pre.CICodeFormatter code{ color:#000000; word-wrap:normal; }

 using System;   
using System.Net;
public bool UploadFile(string localFilePath, string targetUrl, string fileName, out string errorDescription)
{
bool result = false;
errorDescription = string.Empty;
string userName = "UserName";
string password = "password";
string domain = "dominiotUtente";
WebClient webClient = null;
Uri targetUri = new Uri(string.Concat(targetUrl, "/", fileName));
try
{
webClient = new WebClient();
webClient.UseDefaultCredentials = false;
webClient.Credentials = new NetworkCredential(userName, password, domain);
webClient.Proxy.Credentials = new NetworkCredential(userName, password, domain);
//Questo forza l'autenticazione in FORM BASED AUTH ACCEPTED
webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
}
catch (Exception ex)
{
errorDescription = string.Format("WebClient Authentication: {0}", ex.Message);
return false;
}
webClient.UploadProgressChanged += (object sender, UploadProgressChangedEventArgs e) =>
{
Console.WriteLine(e.ProgressPercentage + " %");
};
try
{
//Upload effettivo del file
webClient.UploadFile(targetUri, "PUT", localFilePath);
result = true;
}
catch (Exception ex)
{
errorDescription = string.Format("WebClient UploadFile: {0}", ex.Message);
result = false;
}
return result;
}

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *