I am programming an VSTO Word Addin. This addin read information (list) from sharepoint online.
How can I get Credentials from ActiveDocument class to create a ContextClient
?
I tried to use System.Net.CredentialCache.DefaultCredentials
ClientContext context = new ClientContext(url);
context.AuthenticationMode = ClientAuthenticationMode.Default;
context.Credentials = System.Net.CredentialCache.DefaultCredentials;
Web web = context.Web;
context.Load(web);
context.Load(web.CurrentUser);
context.ExecuteQuery();
Web rootWeb = context.Site.RootWeb;
context.Load(rootWeb, rw => rw.Url);
context.ExecuteQuery();
rootContextUrl = context.Site.RootWeb.Url;
How can I make it run with Sharepoint on line. It works in Sharepoint on premise.
Thanks
Related
I am trying to connect to a Sharepoint site which I can open & navigate in a browser, so I do have permissions. But in my .NET Framework 4.7.2 Console app, I am getting "401 unauthorized". What am I missing? I am using the "Microsoft.SharePoint.Client.Online.CSOM" nuget package, not sure if that's the right one, but only there I had the "SharePointOnlineCredentials" which other posts recommend to use.
var siteUrl = "https://mycompany.sharepoint.com";
var context = new ClientContext(siteUrl);
var securePassword = new NetworkCredential("", "mypw").SecurePassword;
context.Credentials = new SharePointOnlineCredentials("myusername#mydomain", securePassword);
var web = context.Web;
context.Load(web);
context.ExecuteQuery(); // 401 UNAUTHORIZED
When I am using the "Microsoft.SharePointOnline.CSOM" package instead, I am getting this error:
"Cannot contact web site 'https://mycompany.sharepoint.com/' or the web site does not support SharePoint Online credentials. The response status code is 'Unauthorized'. The response headers are 'X-SharePointHealthScore=0, X-MSDAVEXT_Error=917656; Access+denied.+Before+opening+files+in+this+location%2c+you+must+first+browse+to+the+web+site+and+select+the+option+to+login+automatically"
Until now, I couldn't find anything that helped.
Do I need to register the app in Azure AD or is that only needed for .NET Standard?
It's now working with the "PnP.Framework" nuget package and this code:
var credentials = new System.Net.NetworkCredential("myusername#mydomain", "mypw");
var authManager = new AuthenticationManager(credentials.UserName, credentials.SecurePassword);
using (var context = authManager.GetContext("https://mycompany.sharepoint.com"))
{
var web = context.Web;
context.Load(web, w => w.Id, w => w.Title, w => w.Url);
context.ExecuteQuery();
}
"Operation has timed out" error while connecting to Office 365 Sharepoint from asp.net web application
I have tried finding answers and implementing solutions like below:
How to connect to SharePoint on Office 365 with CSOM from C#?
Also some blogs suggested making an asynchronous query, which does not throw error but also does not give any results.
Also tried setting timeout property without any help.
Below is my code:
SharePointOnlineCredentials networkCredential = new
SharePointOnlineCredentials(SharePointUser, SharePointPassword);
Context = new ClientContext(SharePointURL);
Context.Credentials = networkCredential;
Web = Context.Web;
Context.Load(Web);
Context.ExecuteQuery();`
Also, strangely I am able to connect and get data using Console application, but I need to get this working in web application.
After a lot of search I realized that we need proxy to connect to Sharepoint Online and implemented following code to achieve
clientContext.ExecutingWebRequest += (s, e) =>
{
e.WebRequestExecutor.WebRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
};
Add clientContext.RequestTimeout = -1 in the code, the code below for your reference.
string siteUrl = "https://tenant.sharepoint.com/sites/lz";
string userName = "lz#tenant.onmicrosoft.com";
string password = "xxx";
var securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
using (ClientContext clientContext = new ClientContext(siteUrl))
{
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
clientContext.RequestTimeout = -1;
var web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
}
I need to save files from the existing AngularJS/.NET application to Sharepoint. Most of the examples I see online is when applications reside on Sharepoint itself. How do I save files from outside?
I've been given a user access to our organization's Sharepoint site but no application user passwords. What do I need to request from administrators of SharePoint site to be able to write the code?
We can use CSOM C# code to upload file to SharePoint 2010 document library. We need use an admin user and password to pass the Credentials in the .NET application server.
public static void UploadFile(ClientContext context, string uploadFolderUrl, string uploadFilePath)
{
var fileCreationInfo = new FileCreationInformation
{
Content = System.IO.File.ReadAllBytes(uploadFilePath),
Overwrite = true,
Url = Path.GetFileName(uploadFilePath)
};
var targetFolder = context.Web.GetFolderByServerRelativeUrl(uploadFolderUrl);
var uploadFile = targetFolder.Files.Add(fileCreationInfo);
context.Load(uploadFile);
context.ExecuteQuery();
}
Usage
var siteUrl="http://sp2010";
var username="admin";
var password="xx";
var domainName="domain1";
using (var ctx = new ClientContext(webUri))
{
ctx.Credentials = new System.Net.NetworkCredential(username, password, domainName);
UploadFile(ctx,"Documents/folder1",#"c:\upload\test.docx");
}
The following article for your reference.
Uploading files using Client Object Model in SharePoint 2010
I'm new to sharepoint. Im trying to upload a file and add metadata to the file. Below is my code. I can see some internal exceptions are their while declaring ClientContext
Exception: clientContext.ServerVersion threw an exception of type PropertyOrFieldNotInitializedException
The same is happening for Site, Web.
using (ClientContext clientContext = new ClientContext(SPURL))
{
//ClientContext clientContext = new ClientContext(SPURL);
clientContext.Credentials = new System.Net.NetworkCredential(userName, password, domain);
Web web = clientContext.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(PPTPath);
newFile.Url = "F:\\log.txt";
List docs = web.Lists.GetByTitle("Test");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
clientContext.ExecuteQuery();
using (FileStream fileStream =
new FileStream(PPTPath, FileMode.Open))
ClientOM.File.SaveBinaryDirect(clientContext,
DocumentRepository + PPTfilename, fileStream, true);
}
the CSOM libraries expect you to tell them what you want to query from the SharePoint environment upon calling ExecuteQuery().
Therefor you need to tell the clientcontext object what you want to load.
For instance:
Web web = clientContext.Web; should be followed by clientContext.Load(web);
This is a simple example. For uploading files your best bet is making use of the File.SaveBinaryDirect function. An example on how to use it can be found elsewhere on stackoverflow.
I have just started using Sharepoint Foundation 2010 and I'm trying to write a function from c# to add pages to a site.
I got some code working to create a new site, but I can't seem to find any documentation about adding a page to an existing site using the client object model.
This is probably a simple question but if anyone can help me out I would appreciated it.
Thanks.
Update
This is what I have so far:
private void createPage()
{
ClientContext context = new ClientContext(url);
Site siteCollection = context.Site;
Web site = context.Web;
List pages = site.Lists.GetByTitle("Pages");
FileCreationInformation fileCreateInfo = new FileCreationInformation();
fileCreateInfo.Url = "NewPage";
fileCreateInfo.Content = System.Text.Encoding.ASCII.GetBytes("Test");
context.Load(pages.RootFolder.Files.Add(fileCreateInfo));
context.ExecuteQuery();
context.Dispose();
}
But I get a server exception "List 'Pages' does not exist at site with URL"
This is what I eventually did to add my page. Essentially I just needed to find the appropriate list title. These are just the names of the Document Libraries on the site.
private void createPage()
{
ClientContext context = new ClientContext(URL);
Site siteCollection = context.Site;
Web site = context.Web;
List pages = site.Lists.GetByTitle("Site Pages");
Microsoft.SharePoint.Client.
FileCreationInformation fileCreateInfo = new FileCreationInformation();
fileCreateInfo.Url = "NewPage.aspx";
context.Load(pages.RootFolder.Files.Add(fileCreateInfo));
context.ExecuteQuery();
context.Dispose();
}
If you are talking about creating a new site page I would recommend looking at this tutorial:
http://blogs.msdn.com/b/kaevans/archive/2010/06/28/creating-a-sharepoint-site-page-with-code-behind-using-visual-studio-2010.aspx
Take a second and make sure you actually want to add this through code. As someone who recently started development with SharePoint I can tell you that there is a fairly steep learning curve when working with the Object Model. Also, tasks that are easy to do through the UI may be quite difficult using code.
Good luck!!!
This Code works for me. It Creates an Page("NewPage.aspx") With the Content Test.
private void createPage()
{
ClientContext context = new ClientContext(URL);
Site siteCollection = context.Site;
Web site = context.Web;
List pages = site.Lists.GetByTitle("Site Pages");
Microsoft.SharePoint.Client.
FileCreationInformation fileCreateInfo = new FileCreationInformation();
fileCreateInfo.Url = "NewPage.aspx";
fileCreateInfo.Content = System.Text.Encoding.ASCII.GetBytes("Test");
context.Load(pages.RootFolder.Files.Add(fileCreateInfo));
context.ExecuteQuery();
context.Dispose();
}