locked
SharePoint 2013 RRS feed

  • Question

  • Hi,

    If would like to upload files remotely to SharePoint2013 Online document library using a .Net application. Do we need to install some service / software on our SharePoint Online tenant?


    Regards,

    Kiran Pedduri

    <style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Helvetica} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Helvetica; color: #0000ee} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Helvetica; min-height: 13.0px} span.s1 {font-kerning: none} span.s2 {font-kerning: none; color: #000000} span.s3 {text-decoration: underline ; font-kerning: none} span.s4 {text-decoration: underline ; font-kerning: none; background-color: #ff0000} span.s5 {font-kerning: none; background-color: #ff0000} span.s6 {text-decoration: underline ; font-kerning: none; color: #0000ee} </style>
    Wednesday, March 8, 2017 9:31 AM

Answers

  • Hi,

    Below code works fine based on my test.

    param(
    [string] $CTName = "Document",
    [string] $SiteURL = "https://xxx.sharepoint.com/sites/lee",
    [string] $DocLibName = "MyDoc2",
    [string] $User = "user@xxx.onmicrosoft.com",
    [string] $Folder = "C:\Lee\Share"
    )
    #Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    $Password = Read-Host -Prompt "Please enter your password"  -AsSecureString
    #Param Messages
    Write-Host "Document Library :"  $DocLibName
    Write-Host "Destination Site URL :"  $SiteURL
    #Bind to site collection
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
    $Context.Credentials = $Creds
    Write-Host "Credentials verified successfully!!"
    Write-Host "In Progress.."
    #Retrieve list
    $List = $Context.Web.Lists.GetByTitle($DocLibName)
    $Context.Load($List)
    $Context.ExecuteQuery()
    Write-Host "Document Library found"
    #Upload file
    Foreach ($File in  (dir $Folder -File))
    {
    $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $File
    $Upload = $List.RootFolder.Files.Add($FileCreationInfo)
    Write-Host "In Progress."
    $Context.Load($Upload)
    $Context.Load($List.ContentTypes)
    $Context.ExecuteQuery()
    $item = $Upload.ListItemAllFields
    Write-Host "File name "$File" have been uploaded successfully!!"
    $item["Title"] = $File.BaseName
    $newCTID="0";
    foreach($ct in  $List.ContentTypes)
    {
    if($ct.Name.ToUpper() -eq $CTName.ToUpper())
    {
    write-host $ct.Name -Foregroundcolor Green
    write-host $ct.ID -Foregroundcolor Red
    $newCTID = $ct.ID
    }
    }
    #Association of content type with uploading item
    Write-Host "CT ID: " $newCTID -Foregroundcolor Yellow
    $item["ContentTypeId"] =$newCTID
    $item.Update()
    $Context.ExecuteQuery()
    Write-Host "Item is inserted to destination list successfully!!"
    }
    Write-Host "Completed uploading"

    Make sure you installed all the components needed as below.

    https://social.technet.microsoft.com/wiki/contents/articles/34755.sharepoint-online-powershell-and-csom-remote-machine-environment-setup-requirement.aspx

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, March 9, 2017 6:10 AM

All replies

  • I have tried below approach, Facing issues please see the error mentioned below for more information.
    
    https://social.technet.microsoft.com/wiki/contents/articles/34603.o365-sharepoint-online-upload-your-files-remotely-using-powershell-and-csom.aspx
    
    Error Message while executing powershell script
    
    PS C:\Users\kiranped\Downloads> .\BulkFileUploaderTool.ps1
    Please enter your password: **********
    Document Library : Test
    Destination Site URL : https://efi365.sharepoint.com/sites/QA/SitePages/Home.aspx
    Credentials verified successfully!!
    In Progress..
    Exception calling "ExecuteQuery" with "0" argument(s): "Cannot contact site at the specified URL
    https://efi365.sharepoint.com/sites/QA/SitePages/Home.aspx. There is no Web named
    "/sites/QA/SitePages/Home.aspx/_vti_bin/sites.asmx"."
    At C:\Users\kiranped\Downloads\BulkFileUploaderTool.ps1:74 char:1
    + $Context.ExecuteQuery()
    + ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ClientRequestException
    Document Library found
    New-Object : Exception calling ".ctor" with "2" argument(s): "The process cannot access the file
    'C:\FilesToUpload\Sampleapp.txt' because it is being used by another process."
    At C:\Users\kiranped\Downloads\BulkFileUploaderTool.ps1:82 char:15
    + $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open ...
    +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
    In Progress...
    Exception calling "ExecuteQuery" with "0" argument(s): "Cannot contact site at the specified URL
    https://efi365.sharepoint.com/sites/QA/SitePages/Home.aspx. There is no Web named
    "/sites/QA/SitePages/Home.aspx/_vti_bin/sites.asmx"."
    At C:\Users\kiranped\Downloads\BulkFileUploaderTool.ps1:93 char:1
    + $Context.ExecuteQuery()
    + ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ClientRequestException
    File name  Sampleapp.txt have been uploaded successfully!!
    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.
    At C:\Users\kiranped\Downloads\BulkFileUploaderTool.ps1:100 char:9
    + foreach($ct in $List.ContentTypes)
    +         ~~~
        + CategoryInfo          : OperationStopped: (:) [], CollectionNotInitializedException
        + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException
    CT ID:  0
    Exception calling "ExecuteQuery" with "0" argument(s): "Cannot contact site at the specified URL
    https://efi365.sharepoint.com/sites/QA/SitePages/Home.aspx. There is no Web named
    "/sites/QA/SitePages/Home.aspx/_vti_bin/sites.asmx"."
    At C:\Users\kiranped\Downloads\BulkFileUploaderTool.ps1:114 char:1
    + $Context.ExecuteQuery()
    + ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ClientRequestException
    ProcessDropOffLibraryItem in Progress...
    ProcessDropOffLibraryItem : Unable to find type [Microsoft.SharePoint.SPWeb]: make sure that the assembly containing
    this type is loaded.
    At C:\Users\kiranped\Downloads\BulkFileUploaderTool.ps1:117 char:1
    + ProcessDropOffLibraryItem -web $Context.Web -item $item -listRequiresModeration  ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (Microsoft.SharePoint.SPWeb:TypeName) [], RuntimeException
        + FullyQualifiedErrorId : TypeNotFound
    Title is updated!!
    Completed uploading
    PS C:\Users\kiranped\Downloads>
    PS C:\Users\kiranped\Downloads> .\BulkFileUploaderTool.ps1
    Please enter your password: **********
    Document Library : Test
    Destination Site URL : https://efi365.sharepoint.com/sites/QA/SitePages/Home.aspx?RootFolder=%2Fsites%2FQA%2FShared%20Do
    cuments%2FTest&FolderCTID=0x012000C0C3A58AACC0BE4299C69A65E831E0AA&View=%7BC01DCCD6-37F6-4574-AE4E-7DEA7C9524F8%7D
    Credentials verified successfully!!
    In Progress..
    Exception calling "ExecuteQuery" with "0" argument(s): "Cannot contact site at the specified URL https://efi365.sharepo
    int.com/sites/QA/SitePages/Home.aspx?RootFolder=%2Fsites%2FQA%2FShared%20Documents%2FTest&FolderCTID=0x012000C0C3A58AAC
    C0BE4299C69A65E831E0AA&View=%7BC01DCCD6-37F6-4574-AE4E-7DEA7C9524F8%7D."
    At C:\Users\kiranped\Downloads\BulkFileUploaderTool.ps1:74 char:1
    + $Context.ExecuteQuery()
    + ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ClientRequestException
    Document Library found
    New-Object : Exception calling ".ctor" with "2" argument(s): "The process cannot access the file
    'C:\FilesToUpload\Sampleapp.txt' because it is being used by another process."
    At C:\Users\kiranped\Downloads\BulkFileUploaderTool.ps1:82 char:15
    + $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open ...
    +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
    In Progress...
    Exception calling "ExecuteQuery" with "0" argument(s): "Cannot contact site at the specified URL https://efi365.sharepo
    int.com/sites/QA/SitePages/Home.aspx?RootFolder=%2Fsites%2FQA%2FShared%20Documents%2FTest&FolderCTID=0x012000C0C3A58AAC
    C0BE4299C69A65E831E0AA&View=%7BC01DCCD6-37F6-4574-AE4E-7DEA7C9524F8%7D."
    At C:\Users\kiranped\Downloads\BulkFileUploaderTool.ps1:93 char:1
    + $Context.ExecuteQuery()
    + ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ClientRequestException
    File name  Sampleapp.txt have been uploaded successfully!!
    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.
    At C:\Users\kiranped\Downloads\BulkFileUploaderTool.ps1:100 char:9
    + foreach($ct in $List.ContentTypes)
    +         ~~~
        + CategoryInfo          : OperationStopped: (:) [], CollectionNotInitializedException
        + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException
    CT ID:  0
    



    Wednesday, March 8, 2017 9:34 AM
  • Hi,

    Below code works fine based on my test.

    param(
    [string] $CTName = "Document",
    [string] $SiteURL = "https://xxx.sharepoint.com/sites/lee",
    [string] $DocLibName = "MyDoc2",
    [string] $User = "user@xxx.onmicrosoft.com",
    [string] $Folder = "C:\Lee\Share"
    )
    #Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    $Password = Read-Host -Prompt "Please enter your password"  -AsSecureString
    #Param Messages
    Write-Host "Document Library :"  $DocLibName
    Write-Host "Destination Site URL :"  $SiteURL
    #Bind to site collection
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
    $Context.Credentials = $Creds
    Write-Host "Credentials verified successfully!!"
    Write-Host "In Progress.."
    #Retrieve list
    $List = $Context.Web.Lists.GetByTitle($DocLibName)
    $Context.Load($List)
    $Context.ExecuteQuery()
    Write-Host "Document Library found"
    #Upload file
    Foreach ($File in  (dir $Folder -File))
    {
    $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $File
    $Upload = $List.RootFolder.Files.Add($FileCreationInfo)
    Write-Host "In Progress."
    $Context.Load($Upload)
    $Context.Load($List.ContentTypes)
    $Context.ExecuteQuery()
    $item = $Upload.ListItemAllFields
    Write-Host "File name "$File" have been uploaded successfully!!"
    $item["Title"] = $File.BaseName
    $newCTID="0";
    foreach($ct in  $List.ContentTypes)
    {
    if($ct.Name.ToUpper() -eq $CTName.ToUpper())
    {
    write-host $ct.Name -Foregroundcolor Green
    write-host $ct.ID -Foregroundcolor Red
    $newCTID = $ct.ID
    }
    }
    #Association of content type with uploading item
    Write-Host "CT ID: " $newCTID -Foregroundcolor Yellow
    $item["ContentTypeId"] =$newCTID
    $item.Update()
    $Context.ExecuteQuery()
    Write-Host "Item is inserted to destination list successfully!!"
    }
    Write-Host "Completed uploading"

    Make sure you installed all the components needed as below.

    https://social.technet.microsoft.com/wiki/contents/articles/34755.sharepoint-online-powershell-and-csom-remote-machine-environment-setup-requirement.aspx

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, March 9, 2017 6:10 AM