0

I have to specifically use C#. I am able to upload files to Root folder.

  • You should provide your existing code and error messages. – MHeld Sep 12 at 15:57
0

I think you can create Console application with C# or use Powershell. I think the Powershell way is too easy and there are lot of articles available in web.

 #Specify tenant admin and site URL  
$User = "Your id"  
$Password = 'Your Password'  
$SiteURL = "Site URL/"  
$Folder = "C:\Folder\Folderinside"  
#Path where you want to Copy  
$DocLibName = "Documents"  
#Docs library  
# 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\16\ISAPI\Microsoft.SharePoint.Client.dll"  
Add - Type - Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"#  
Bind to site collection  
$Context = New - Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
$Creds = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, (ConvertTo - SecureString $Password - AsPlainText - Force))  
$Context.Credentials = $Creds  
Retrieve list  
$List = $Context.Web.Lists.GetByTitle($DocLibName)  
$Context.Load($List)  
$Context.ExecuteQuery()  
# 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)  
    $Context.Load($Upload)  
    $Context.ExecuteQuery()  
}  

with folder

function UploadImages($weburl)
{
$docLibraryName = "TestLibrary"
$localFolderPath = "c:\Install\Docs"
$subFolderName="SubFolder" ##Pass your Folder Name Here

Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue 
$web = Get-SPWeb -Identity $webUrl

$docLibrary = $web.Lists[$docLibraryName] 
#Attach to local folder and enumerate through all files
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() | ForEach-Object { 

    #Create file stream object from file
    $fileStream = ([System.IO.FileInfo] (Get-Item $_.FullName)).OpenRead()
    $contents = new-object byte[] $fileStream.Length
    $fileStream.Read($contents, 0, [int]$fileStream.Length);
    $fileStream.Close(); 

    write-host "Copying" $_.Name "to" $docLibrary.Title "in" $web.Title "..." 

    #Add file
    $folder = $web.getfolder($docLibrary.Title+"/"+$subFolderName) ##Changed code here to get subfolder
    $spFile = $folder.Files.Add($folder.Url + "/" + $_.Name, $contents, $true)
    $spItem = $spFile.Item 



Write-Host -f Green "Added Images to library !!!"
}
}

Example with C# click here

  • Please suggest me with C#. I am already halfway with C#. Plus how to navigate to the sharepoint folders? This script is also uploading files to Document Library. What if I have to upload file s to Folder named "Admin" present in Documents library? – Shubham Srivastava Sep 12 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.