I have a requirement to download files (Excel) from Sharepoint online to local directory using "SSIS". Please advise on the options available.
I am new to SSIS
you cannot connect to sharepoint online site using SSIS.
you should try using SharePoint CSOM or SharePoint JSOM to achieve that.
Get SharePoint online SDK from
https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM/
Then, you could use CSOM to access SharePoint.
https://salvoz.com/posts/2014-05-23-connect-to-sharepoint-online-list-via-csom-in-ssis.html
Sample code to access SharePoint file by CSOM.
using (ClientContext clientContext = GetContextObject())
{
Web web = clientContext.Web;
clientContext.Load(web, website => website.ServerRelativeUrl);
clientContext.ExecuteQuery();
Regex regex = new Regex(SiteUrl, RegexOptions.IgnoreCase);
string strSiteRelavtiveURL = regex.Replace(FileUrl, string.Empty);
string strServerRelativeURL = CombineUrl(web.ServerRelativeUrl, strSiteRelavtiveURL);
Microsoft.SharePoint.Client.File oFile = web.GetFileByServerRelativeUrl(strServerRelativeURL);
clientContext.Load(oFile);
ClientResult<Stream> stream = oFile.OpenBinaryStream();
clientContext.ExecuteQuery();
return this.ReadFully(stream.Value);
}
https://code.msdn.microsoft.com/office/file-from-SharePoint-Online-cc418dba
Related
I have
Registered Azure Application
that have full control permissions to the SharePoint site
and these variables
SharePoint Site Url
TenantId
ClientId
ClientSecret
I need to upload a document to the SharePoint Site Folder.
I tried to use PnP Core SDK but I am not able to configure the Authentication, it seems that there is no authentication provider to just accept plain password (UsernamePasswordAuthenticationProvider does not accept name of the application as a username).
Overall the PnP Core SDK is adding a lot of complexity to my console application because it depends on Microsoft.Extensions.Hosting.Host.
is there a way how to authenticate via PnP or should I use REST API directly?
Alternatively the PnP Framework that will be deprecated (if I understand the documentation correctly) can authenticate towards Azure Application, but this is only documentation I found.
Any idea or recommendation?
Update
When I try this (PnP Framework)
using Microsoft.SharePoint.Client;
using PnP.Core.Model.SharePoint;
using PnP.Framework;
ClientContext context =
new AuthenticationManager()
.GetACSAppOnlyContext(
siteUrl: "siteUrl",
appId: "clientId",
appSecret: "password");
IFolder? folder = (IFolder?)context.Web.Folders.Where(f => f.Name == directory).FirstOrDefault();
if (folder == null) throw new Exception("Folder not found.");
folder.Files.Add(filename, content, overwrite);
I am getting this exception
Microsoft.SharePoint.Client.CollectionNotInitializedException: 'The
collection has not been initialized. It has not been requested or the
request has not been executed. It may need to be explicitly
requested.'
Any Idea how to explicitly request the collection?
According to my research and testing, if you want to connect to SharePoint Online with Azure App credentials, you can use the following code, and then upload file to SharePoint:
string siteUrl = "https://contoso.sharepoint.com/sites/demo";
using (var cc = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, "[Your Client ID]", "[Your Client Secret]"))
{
cc.Load(cc.Web, p => p.Title);
cc.ExecuteQuery();
Console.WriteLine(cc.Web.Title);
};
Here is a document about upload file to SharePoint, you can refer to the code in this document: Upload a document to a SharePoint list from Client Side Object Model
Also, you can try to install Microsoft.SharePointOnline.CSOM to fix the error:
Microsoft.SharePoint.Client.CollectionNotInitializedException: 'The
collection has not been initialized. It has not been requested or the
request has not been executed. It may need to be explicitly
requested.'
More information for reference: Granting access using SharePoint App-Only
Create ClientContext
using Microsoft.SharePoint.Client;
using PnP.Framework;
ClientContext _context =
new AuthenticationManager()
.GetACSAppOnlyContext(
siteUrl: siteUrl,
appId: appId,
appSecret: appSecret);
Method for uploading the file
public void UploadFile(Stream stream, string listTitle, string directory, string filename, bool overwrite)
{
List list = _context.Web.Lists.GetByTitle(listTitle);
var url = Path.Combine(directory, filename);
var file = new FileCreationInformation() { ContentStream = stream, Overwrite = overwrite, Url = url };
var addedFile = list.RootFolder.Files.Add(file);
_context.Load(addedFile);
_context.ExecuteQuery();
}
Call example
UploadFile(stream, "Documents", "Shared Documents/FooSubFolder/", "filename.txt", true)
Using data connection in SharePoint 2013, or any other method, is it possible to submit form data with each form completion to an external landing place, like a list in SharePoint 365 online? I am new to SharePoint and am needing to learn the ropes pretty quick! Thanks for any help you can provide.
Have a great 2020!
You could develop a custom WebPart or Add-in for SP 2013 and in c# code of this project You could, using SPOnline CSOM, add items to SP Online.
The main problem is the authentication model You are using in SP-OnPrem and SP-online (OF365 tenant). If You have the same kind of authorization provider (for example in both places You are using adfs) then there should not be much of a problem to (as the same user account) submit some data from OnPrem to Online with some Add-in or Webpart as the user will be authenticated in both places. Otherwise You can submit some data from OnPrem to Online with CSOM but You will need to login (in code.. like hardcoded) as some user. This is not the best option from security point of view.
Here is an example how to login to SP Online using CSOM (You could do something very similar in the webpart or Add-in).
... so this are only ideas but generally to do this kind of logic first You need to figure out how You authenticate between OnPrem and Online. If You authenticate the 'same way' You could just create the context of SPOnline and then add/delete or update items with the user permissions. Otherwise You need to authenticate to SPOnline as some user to submit data.
If you use the OOTB list form, we can create an event receivers and use CSOM C# code to add new item to SharePoint Online list. The following code for your reference.
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Security;
using Microsoft.SharePoint.Client;
namespace SharePointProjectER.CustomListER
{
/// <summary>
/// List Item Events
/// </summary>
public class CustomListER : SPItemEventReceiver
{
/// <summary>
/// An item was added.
/// </summary>
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
if (properties.List.Title == "CL0106")
{
//add item to SharePoint Online list
string siteUrl = "https://tenant.sharepoint.com/sites/team";
string userName = "admin#tenant.onmicrosoft.com";
string password = "xxx";
var securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
var credential = new SharePointOnlineCredentials(userName, securePassword);
ClientContext clientContext = new ClientContext(siteUrl);
clientContext.Credentials = credential;
List oList = clientContext.Web.Lists.GetByTitle("CL0106");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);
oListItem["Title"] = properties.ListItem["Title"];
oListItem.Update();
clientContext.ExecuteQuery();
}
}
}
}
Or we can create a custom web part with custom form or add-in as Adam's reply, and use the CSOM C# code to achieve it.
Reference: Complete basic operations using SharePoint client library code
This code snippet
string strUserName="abc";
string strPassword="123";
SecureString ssPwd = new SecureString();
strPassword.ToList().ForEach(ssPwd.AppendChar);
ClientContext context = new ClientContext(siteurl);
SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(strUserName, ssPwd);
context.Credentials = credentials;
// The SharePoint web at the URL.
Web web = context.Web;
// We want to retrieve the web's properties.
context.Load(web);
// Execute the query to the server.
context.ExecuteQuery();
Creates the following error message trying to connect to SharePoint Online:
For security reasons DTD is prohibited in this XML document. To enable DTD processing set the DtdProcessing property on XmlReaderSettings to Parse and pass the settings into XmlReader.Create method.
The same thing happens using the SharePoint Client Browser downloaded from CodePlex (https://spcb.codeplex.com).
What's wrong? What can I do?
Working with a SharePoint 2010 Visual Studio workflow that will copy data from a local farm to a remote farm using web services.
I'm having trouble trying to parse an input URL to get the corresponding web on the remote farm.
Example inputs:
https://test2.remotefarm.com/randomweb/webweb/Shared%20Documents/Forms/AllItems.aspx
https://test3.remotefarm.com/
I have no control over what the input will be, so it could be the root web app or five webs deep.
Once I have a valid web URL, I will be appending a web service endpoint.
Edit - This is what I ended up using
Uri fileUrl = new Uri("https://test2.remotefarm.com/randomweb/webweb/Shared%20Documents/Forms/AllItems.aspx");
//Websref is a Web Reference pointing to \webs.aspx
websRef.Webs webs1 = new websRef.Webs();
webs1.Url = "https://" + fileUrl.Authority + "/" + sWebsSuffix;
webs1.UseDefaultCredentials = true;
string strWebUrl = webs1.WebUrlFromPageUrl(fileUrl.ToString());
You can use SPSite to get the site collection from the URL and then get the Web app using OpenWeb(). For example:
using (SPSite siteCollection = new SPSite("https://test2.remotefarm.com/randomweb/webweb/Shared%20Documents/Forms/AllItems.aspx"))
{
SPWeb myWeb = siteCollection.OpenWeb();
//do stuff with myWeb here
}
I have used Javascript and sharepoint client object model to add a list item to a list. I am working with a sharepoint 2013 online public site. I have a content editor with the following javascript code.
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle('Subscriptions');
var email = document.getElementById('email').value;
// Create a new list item
var itemCreateInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(itemCreateInfo);
listItem.set_item('Title',email );
listItem.update();
This code works fine for the user who has permission. it will add a element to the subscription list. But it fails when we use this for a Anonymous user. After searching I found that that there is a tool : http://anonymous365.codeplex.com/.
But it did not work from the code though i gave anonymous access to the list.
Please suggest me a way to overcome this.
Thanks,
Access to SharePoint Online requires an authenticated session of some variety. The only site that is available for public (anonymous) access is the pre-provisioned "public site" that is available with certain subscriptions which is meant to host a standard corporate website. A sample project that shows how to support claims authentication with SPO can be found here: http://code.msdn.microsoft.com/office/Remote-Authentication-in-b7b6f43c/