I am trying to retrieve some information from a Office 365 site. I am getting a inconsistent 403 error on executeQuery call. This happens irregularly and is solved by IISReset. Please help.
ClientContext clientContext = new ClientContext(Constants.SP_URL);
{
SecureString passWord = new SecureString();
foreach (char c in Constants.SP_SERVICE_PASS.ToCharArray()) passWord.AppendChar(c);
var cred = new SharePointOnlineCredentials(Constants.SP_SERVICE_ACC, passWord); ;
clientContext.Credentials = new SharePointOnlineCredentials(Constants.SP_SERVICE_ACC, passWord);
Web web = clientContext.Web;
string docLibraryName = Constants.SP_PUBLISHED_LIB;
var list = clientContext.Web.Lists.GetByTitle(docLibraryName);
clientContext.Load(list);
**clientContext.ExecuteQuery();**
Here is what you need to get a client context and load your data.This authenticate you against SP Online in office 365. I do not know how would you say i reset my IIS. This is not applicable unless you connect to a local SP instance.
private void AutheticateO365(string url, string password, string userName)
{
Context = new ClientContext(url);
var passWord = new SecureString();
foreach (char c in password.ToCharArray()) passWord.AppendChar(c);
Context.Credentials = new SharePointOnlineCredentials(userName, passWord);
var web = Context.Web;
Context.Load(web);
Context.ExecuteQuery();
}
Related
I would like to connect a SharePoint Online with the current user on .NET with Single Sign On. I don't want to specify username and password in my code. Unfortunatly, I've the following error message on ExecuteQuery() :
The remote server returned an error: (403) FORBIDDEN
My code :
string siteCollectionUrl = "https://xxx.sharepoint.com/teams/yyyy";
System.Net.ICredentials credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
SharePoint.ClientContext context = new SharePoint.ClientContext(siteCollectionUrl);
context.Credentials = credentials;
SharePoint.Web web = context.Web;
context.Load(web);
context.ExecuteQuery();
string tt = web.Title;
Do you have an idea ?
Thanks in advance
You have to use SharePointOnlineCredentials class for authentication.
Like this:
String name = "user#xxx.onmicrosoft.com";
String password = "xxxx";
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray())
{
securePassword.AppendChar(c);
}
var credentials = new SharePointOnlineCredentials(name, securePassword);
string siteCollectionUrl = "https://xxx.sharepoint.com/teams/yyyy";
ClientContext ctx = new ClientContext(siteCollectionUrl );
ctx.Credentials = credentials;
I'm using the below code for sharepoint authentication. It works perfectly fine on a local machine, but when I deploy this package on server it is giving 401 error.
class Program
{
static void Main(string[] args)
{
//Delcaring Required variable (Actual variable values have been changed to dummy one)
var webUri = new Uri("https://blahblah.sharepoint.com");
string proxy = "http://abc.abc.com:1234/";
const string userName = "abc#blahblah.com";
const string password = "123";
string SalesPOV_GUID = "6319bd1f-7f-4ffd-95dc-a992afc4da10";
string Clients_GUID = "ojfoeiawe-3abcc6-41cd-be23-cf6043671d53";
//Creating a secured sring for SharepointOnline Credentials
var securePassword = new SecureString();
foreach (var c in password)
{
securePassword.AppendChar(c);
}
//Setting up credentials for Sharepoint
var credentials = new SharePointOnlineCredentials(userName, securePassword);
//Makiing a Call
using (var client = new WebClient())
{
try
{
//setting up proxy
System.Net.WebProxy wp = new System.Net.WebProxy();
Uri newUri = new Uri(proxy);
wp.Address = newUri;
client.Proxy = wp;
}
catch (WebException e) //In package we need to fail the package on catching exception.
{
string pageContent = new StreamReader(e.Response.GetResponseStream()).ReadToEnd().ToString();
}
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Credentials = credentials;
}
}
}
Check if your sharepoint website is set to "pass-through" authentication for the physical path.
I'm triggering an external web page outside of sharepoint which needs to read lists using the sharepoint web api.
HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "/_api/web/lists");
endpointRequest.Method = "GET";
endpointRequest.Accept = "application/json;odata=verbose";
//endpointRequest.Headers.Add("Authorization", "Bearer " + accessToken);
HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
I can access the API using chrome if I'm logged in but I suspect I need the access token line.. but can't seem to find a way to populate it.
Currently it returns:
No connection could be made because the target machine actively refused it...
You need to set credential of HttpWebRequest.
Here you go:
HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "/_api/web/lists");
string password = "XXXXX";
string userName = "XXXX";
SecureString secureString = new SecureString();
foreach (char c in password.ToCharArray())
{
secureString.AppendChar(c);
}
endpointRequest.Credentials = new SharePointOnlineCredentials(userName, secureString);
//.........
If you are using SharePoint online, it can work with my answer above because i have tested.
If you are using SharePoint 2013/2010/2016, the code will be as below.
HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "/_api/web/lists");
string password = "XXXXX";
string userName = "XXXX";
string domain = "XXX"
endpointRequest.Credentials = new NetworkCredential(userName, password, domain);
//.........
I'm trying to connect to a Sharepoint using ClientContext (Microsoft.SharePoint.Client lib).
Unfortunately, when I execute the code that supposed to connect on Sharepoint Site, I'm getting 401 error.
When I try to connect using a web browser it works fine.
Here comes my code:
using (ClientContext clientcontext = new ClientContext("http://mysite/"))
{
var credentials = new NetworkCredential(user, password, domain);
clientcontext.Credentials = credentials;
Web web = clientcontext.Web;
clientcontext.Load(web);
clientcontext.ExecuteQuery();
}
Thanks!
Hi sorry for the delay.
Please see my working code below.
If you are using SharePoint online, use below code
public static ClientContext GetClientContext(string url)
{
SecureString securePassword = new SecureString();
ClientContext context = null;
try
{
using (context = new ClientContext(url))
{
foreach (char c in "Password") securePassword.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials("user#tenent.onmicrosoft.com", securePassword);
context.Load(context.Web, w => w.ServerRelativeUrl, w => w.Url);
context.ExecuteQuery();
}
}
catch (Exception ex)
{
}
return context;
}
If you are using SharePoint on premises server, you can get the context in two ways. Using app pool account or by passing your user creds. Below code using the default app pool credentials.
string parentSiteUrl = Helper.GetParentWebUrl(siteUrl);
clientContext = new ClientContext(parentSiteUrl);
clientContext.Credentials = CredentialCache.DefaultCredentials;
clientContext.Load(clientContext.Web, w => w.Url, w => w.Lists, w => w.ServerRelativeUrl, w => w.Title, w => w.SiteGroups);
clientContext.ExecuteQuery();
Or you can pass your credentials as below.
var clientContext = new ClientContext(siteUrl);
clientContext.Credentials = new NetworkCredential("domain\\user", "password");
clientContext.Load(clientContext.Web, w => w.Lists);
clientContext.ExecuteQuery();
Let me know if you have any queries.
Use "Microsoft.SharePoint.Client.dll" and "Microsoft.SharePoint.Client.Runtime.dll".
using (ClientContext clientcontext = new ClientContext("http://mysite/"))
{
var credentials = new NetworkCredential(domain\UserName, password);
clientcontext.Credentials = credentials;
Web web = clientcontext.Web;
clientcontext.Load(web);
clientcontext.ExecuteQuery();
}
How can one resolve this runtime error?
Error
Could not load file or assembly 'Microsoft.SharePoint.Library, Version = 14.0.0.0, Culture = neutral, PublicKeyToken = 71e9bce111e9429c' or one of its dependencies. The system can not find the file specified.
CODE
string destUrl = "URL";
string destFileUrl = destUrl + "biblioteca" + "/text.txt";
using(SPWeb site = new SPSite(destUrl).OpenWeb())
{
site.AllowUnsafeUpdates = true;
FileStream fileStream = File.Open("FILE" , FileMode.Open);
site.Files.Add(destFileUrl, fileStream, true/*overwrite*/);
fileStream.Close();
}
This works for me.
using (ClientContext clientContext = new ClientContext("SHAREPOINT URL")) {
SecureString passWord = new SecureString();
foreach (char c in "PASSWORD".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("ACOUNT.onmicrosoft.com", passWord);
Web web = clientContext.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(FILE);
newFile.Url = NAMEFORTHEFILE;
List docs = web.Lists.GetByTitle("LIBRARY NAME");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
clientContext.ExecuteQuery();
}
string fileName = #"C:\AddUser.aspx";
using (var context = new ClientContext("https://yourdomain.com")) {
var passWord = new SecureString();
foreach (var c in "YourPassword") passWord.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials("YourUsername", passWord);
var web = context.Web;
var newFile = new FileCreationInformation {
Content = System.IO.File.ReadAllBytes(fileName),
Url = Path.GetFileName(fileName)
};
var docs = web.Lists.GetByTitle("Pages");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
context.ExecuteQuery();
}
Have you encountered this error: The partner returned a bad sign-in name or password error. For more information, see Federation Error-handling Scenarios.
I have the following codes:
NetworkCredential Cred = new NetworkCredential(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"], ConfigurationManager.AppSettings["Domain"]);
SecureString PassWord = new SecureString();
foreach (char c in ConfigurationManager.AppSettings["Password"].ToCharArray()) PassWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(ConfigurationManager.AppSettings["Username"], PassWord);
Web web = clientContext.Web;
clientContext.Load(web);
You cannot use the server side object model to upload files to SharePoint Online.
One has to use the Client Object Model to upload files to SharePoint online.
More Info
Office 365 Sharepoint Upload Files to Documents Library