Upload file to folder or subfolder on Sharepoint - c#

Im trying to create a method to upload a file stream to a sharepoint so far i have this
public static void SPUploadFileStream(string username, string filePath, Stream fileData)
{
//string siteUrl = Configuration.SPSiteURL;
string siteUrl = SPContext.Current.Web.Url;
SPUser currentUser = SPUtils.GetCurrentUser(username);
if (currentUser == null)
{
throw new SPGappUnknownUserException(username);
}
using (SPSite site = new SPSite(siteUrl, currentUser.UserToken))
{
using (SPWeb web = site.OpenWeb())
{
bool allowWebUnsafeUpdt = web.AllowUnsafeUpdates;
if (!allowWebUnsafeUpdt)
web.AllowUnsafeUpdates = true;
try
{
SPCreateFolder(Path.GetDirectoryName(filePath), username);
SPFile newFile = web.Files.Add(filePath, fileData, true); //true = replace
}
catch (Exception ex)
{
LoggingService.LogError(ex);
//site.AllowUnsafeUpdates = allowSiteUnsefaUpdt;
web.AllowUnsafeUpdates = allowWebUnsafeUpdt;
throw new ApplicationException("ERROR "+ ex.ToString());
}
}
}
}
but it works ok if i have a path like "FOLDER/file.jpg" but it doesn't when i have subfolders "FOLDER/SUB/file.jpg"
can anyone give me some pointers?

My guess is that the problem lies inside your SPCreateFolder method. It should have created folders recursively. As when the you try to add new file with
SPFile newFile = web.Files.Add(filePath, fileData, true); //true = replace
the server relative path must exist. Try following method for folder creation
private static void SPCreateFolder(SPWeb web, string filepath)
{
// since you pass this as Path.GetDictionary it's no longer split by '/'
var foldersTree = filepath.Split('\\');
foldersTree.Aggregate(web.RootFolder, GetOrCreateSPFolder);
}
private static SPFolder GetOrCreateSPFolder(SPFolder sourceFolder, string folderName)
{
SPFolder destination;
try
{
// return the existing SPFolder destination if already exists
destination = sourceFolder.SubFolders[folderName];
}
catch
{
// Create the folder if it can't be found
destination = sourceFolder.SubFolders.Add(folderName);
}
return destination;
}
Then you can execute this with
...
SPCreateFolder(web, Path.GetDirectoryName(filePath));
SPFile newFile = web.Files.Add(filePath, fileData, true); //true = replace
...
Let me know if that helps

Related

GetFileByServerRelativePath does not work and always returns file not found

I was previously using the GetFileByServerRelativeUrl and it was working fine, but the characters # and % are not supported while they are supposed to be supported with GetFileByServerRelativePath, so I changed the code as per below but now it just doesn't work with any files???
public bool DownloadFile(string filepath, out string Base64EncodedFile, out string errormessage)
{
Base64EncodedFile = string.Empty;
errormessage = string.Empty;
try
{
Uri filename = new Uri(filepath);
string serverrelative = filename.AbsolutePath;
//This old method does not support # or % but works fine
//Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(serverrelative);
// >> Replaced with this
ResourcePath filePathDecoded = ResourcePath.FromDecodedUrl(serverrelative);
Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativePath(filePathDecoded);
// << Replaced with this
context.Load(file);
ClientResult<System.IO.Stream> streamResult = file.OpenBinaryStream();
context.ExecuteQuery();
Base64EncodedFile = ConvertToBase64(streamResult.Value);
return true;
}
catch (Exception ex)
{
errormessage = ex.Message;
return false;
}
}
SharepointClient.SharepointClient newupload = new SharepointClient.SharepointClient("https://xxxxxxx.sharepoint.com/sites/xxxxxxxxx/", usernametext.Text, textpassword.Text);
newupload.DownloadFile(Url.Text, out EncodedAbs, out errormessage);
If I use the old GetFileByServerRelativeUrl it works just fine... I tried everything but I cannot seem to get to work the GetFileByServerRelativePath ... I can't understand what I'm doing wrong???
Please help!!!
My test code for your reference.
using (ClientContext ctx = new ClientContext(targetSiteURL))
{
ctx.Credentials = onlineCredentials;
string fileName = "FileWith#%.docx";
var _File = ctx.Web.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl($"/sites/lee/MyDoc/{fileName}"));
ctx.Load(_File);
ctx.ExecuteQuery();
Console.Write(_File.ServerRelativeUrl);
Console.WriteLine();
}

Replacing Files in Google Drive UpdateMediaUpload

I have no idea, why the content of the file will not be updated. Has anybody a hint?
The DriveFile.Id is valid. The source File is existing readable. Creating and deleting of files works fine. But not the Update.
I have read the migration from v2 to v3 uses generell the Http-PATCH Method for Update. Is this the answer.
I don't want to delete the file and create a new one.
public long DriveUpdateFile(string fileID, string filename, string description, string parent, string mimeType)
{
int lError = (int)Win32ErrorCode.ERROR_SUCCESS;
if (System.IO.File.Exists(filename))
{
FilesResource.GetRequest get = m_Drive.Files.Get(fileID);
File body = get.Execute();
if(!string.IsNullOrEmpty(description))
body.Description = description;
if (!string.IsNullOrEmpty(mimeType))
body.MimeType = mimeType;
// v3 Sematics
if (!string.IsNullOrEmpty(parent))
{
body.Parents = new List<string>();
body.Parents.Add(parent);
}
try
{
// File's content.
using (System.IO.FileStream sr = System.IO.File.OpenRead(filename))
{
FilesResource.UpdateMediaUpload request = m_Drive.Files.Update(body, body.Id, sr, body.MimeType);
request.Upload();
}
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
lError = e.HResult;
}
}
else
{
Console.WriteLine("File does not exist: " + filename);
lError = (int)Win32ErrorCode.ERROR_FILE_NOT_FOUND;
}
return (lError);
}
Use scopes as below,
string[] scopes = new string[] {
DriveService.Scope.Drive,
DriveService.Scope.DriveFile,
DriveService.Scope.DriveMetadata,
DriveService.Scope.DriveAppdata,
DriveService.Scope.DriveScripts
};//DriveService.Scope.DriveReadonly
and I check above code. the problem is ParentFolderId. please remove this line
body.Parents = new List<string>();
body.Parents.Add(parent);
direct assignment is not allowed in this scope. it should mention with request object like : request.Parent ... bla bla
and try.

C# Upload whole directory using FTP

What I'm trying to do is to upload a website using FTP in C# (C Sharp). So I need to upload all files and folders within a folder, keeping its structure. I'm using this FTP class: http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class for the actual uploading.
I have come to the conclusion that I need to write a recursive method that goes through every sub-directory of the main directory and upload all files and folders in it. This should make an exact copy of my folder to the FTP. Problem is... I have no clue how to write a method like that. I have written recursive methods before but I'm new to the FTP part.
This is what I have so far:
private void recursiveDirectory(string directoryPath)
{
string[] filePaths = null;
string[] subDirectories = null;
filePaths = Directory.GetFiles(directoryPath, "*.*");
subDirectories = Directory.GetDirectories(directoryPath);
if (filePaths != null && subDirectories != null)
{
foreach (string directory in subDirectories)
{
ftpClient.createDirectory(directory);
}
foreach (string file in filePaths)
{
ftpClient.upload(Path.GetDirectoryName(directoryPath), file);
}
}
}
But its far from done and I don't know how to continue. I'm sure more than me needs to know this! Thanks in advance :)
Ohh and... It would be nice if it reported its progress too :) (I'm using a progress bar)
EDIT:
It might have been unclear... How do I upload a directory including all sub-directories and files with FTP?
Problem Solved! :)
Alright so I managed to write the method myslef. If anyone need it feel free to copy:
private void recursiveDirectory(string dirPath, string uploadPath)
{
string[] files = Directory.GetFiles(dirPath, "*.*");
string[] subDirs = Directory.GetDirectories(dirPath);
foreach (string file in files)
{
ftpClient.upload(uploadPath + "/" + Path.GetFileName(file), file);
}
foreach (string subDir in subDirs)
{
ftpClient.createDirectory(uploadPath + "/" + Path.GetFileName(subDir));
recursiveDirectory(subDir, uploadPath + "/" + Path.GetFileName(subDir));
}
}
It works very well :)
I wrote an FTP classe and also wrapped it in a WinForms user control. You can see my code in the article An FtpClient Class and WinForm Control.
I wrote a reusable class to upload entire directory to an ftp site on windows server, the program also renames the old version of that folder (i use it to upload my windows service program to the server).
maybe some need this:
class MyFtpClient
{
protected string FtpUser { get; set; }
protected string FtpPass { get; set; }
protected string FtpServerUrl { get; set; }
protected string DirPathToUpload { get; set; }
protected string BaseDirectory { get; set; }
public MyFtpClient(string ftpuser, string ftppass, string ftpserverurl, string dirpathtoupload)
{
this.FtpPass = ftppass;
this.FtpUser = ftpuser;
this.FtpServerUrl = ftpserverurl;
this.DirPathToUpload = dirpathtoupload;
var spllitedpath = dirpathtoupload.Split('\\').ToArray();
// last index must be the "base" directory on the server
this.BaseDirectory = spllitedpath[spllitedpath.Length - 1];
}
public void UploadDirectory()
{
// rename the old folder version (if exist)
RenameDir(BaseDirectory);
// create a parent folder on server
CreateDir(BaseDirectory);
// upload the files in the most external directory of the path
UploadAllFolderFiles(DirPathToUpload, BaseDirectory);
// loop trough all files in subdirectories
foreach (string dirPath in Directory.GetDirectories(DirPathToUpload, "*",
SearchOption.AllDirectories))
{
// create the folder
CreateDir(dirPath.Substring(dirPath.IndexOf(BaseDirectory), dirPath.Length - dirPath.IndexOf(BaseDirectory)));
Console.WriteLine(dirPath.Substring(dirPath.IndexOf(BaseDirectory), dirPath.Length - dirPath.IndexOf(BaseDirectory)));
UploadAllFolderFiles(dirPath, dirPath.Substring(dirPath.IndexOf(BaseDirectory), dirPath.Length - dirPath.IndexOf(BaseDirectory))
}
}
private void UploadAllFolderFiles(string localpath, string remotepath)
{
string[] files = Directory.GetFiles(localpath);
// get only the filenames and concat to remote path
foreach (string file in files)
{
// full remote path
var fullremotepath = remotepath + "\\" + Path.GetFileName(file);
// local path
var fulllocalpath = Path.GetFullPath(file);
// upload to server
Upload(fulllocalpath, fullremotepath);
}
}
public bool CreateDir(string dirname)
{
try
{
WebRequest request = WebRequest.Create("ftp://" + FtpServerUrl + "/" + dirname);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Proxy = new WebProxy();
request.Credentials = new NetworkCredential(FtpUser, FtpPass);
using (var resp = (FtpWebResponse)request.GetResponse())
{
if (resp.StatusCode == FtpStatusCode.PathnameCreated)
{
return true;
}
else
{
return false;
}
}
}
catch
{
return false;
}
}
public void Upload(string filepath, string targetpath)
{
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(FtpUser, FtpPass);
client.Proxy = null;
var fixedpath = targetpath.Replace(#"\", "/");
client.UploadFile("ftp://" + FtpServerUrl + "/" + fixedpath, WebRequestMethods.Ftp.UploadFile, filepath);
}
}
public bool RenameDir(string dirname)
{
var path = "ftp://" + FtpServerUrl + "/" + dirname;
string serverUri = path;
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.Rename;
request.Proxy = null;
request.Credentials = new NetworkCredential(FtpUser, FtpPass);
// change the name of the old folder the old folder
request.RenameTo = DateTime.Now.ToString("yyyyMMddHHmmss");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (var resp = (FtpWebResponse)request.GetResponse())
{
if (resp.StatusCode == FtpStatusCode.FileActionOK)
{
return true;
}
else
{
return false;
}
}
}
catch
{
return false;
}
}
}
Create an instance of that class:
static void Main(string[] args)
{
MyFtpClientftp = new MyFtpClient(ftpuser, ftppass, ftpServerUrl, #"C:\Users\xxxxxxxxxxx");
ftp.UploadDirectory();
Console.WriteLine("DONE");
Console.ReadLine();
}
Unless you're doing this for fun or self-improvement, use a commercial module. I can recommend one from Chilkat, but I'm sure there are others.
Note: I'm pretty sure this does answer the stated problem, What I'm trying to do is to upload a website using FTP in C# (C Sharp).

Get all documents from a sharepoint document library

I want to retrieve all documents from an SPDocumentLibrary I've tried this way but then I got stucked
using (SPSite mysite = SPContext.Current.Site)
{
using (SPWeb myweb = mysite.OpenWeb())
{
SPDocumentLibrary myDocLib = (SPDocumentLibrary)myweb.Lists["DocLibrary"];
SPList myList = SPContext.Current.List;
SPFileCollection myFiles = myList.;
foreach (SPListItem myItem in myList.Items)
{
//adding each found file to my SPFileCollection
myFiles.Add(myItem.File);
}
}
}
but the SPFileCollection.Add function takes more than the file argument !
Once Again
link here
public static bool getAllDocuments()
{
Console.WriteLine("getAllDocuments debug, START");
bool isOK = false;
string baseUrl = "http://jabdw3421:82/sites/TestSite/";
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(baseUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPDocumentLibrary lib = (SPDocumentLibrary)web.Lists["TestLib"];
IEnumerable<SPFile> allFiles = ExploreFolder(lib.RootFolder);
foreach (SPFile file in allFiles)
{
Console.WriteLine("getAllDocuments debug, File Name : " + file.Name);
Console.WriteLine("getAllDocuments debug, File CharSetName : " + file.CharSetName);
Console.WriteLine("getAllDocuments debug, File SourceLeafName : " + file.SourceLeafName);
}
}
}
});
}
catch (Exception e)
{
Console.WriteLine("getAllDocuments debug, " + e.Message);
isOK = false;
}
Console.WriteLine("getAllDocuments debug, END");
return isOK;
}
private static IEnumerable<SPFile> ExploreFolder(SPFolder folder)
{
foreach (SPFile file in folder.Files)
{
yield return file;
}
foreach (SPFolder subFolder in folder.SubFolders)
{
foreach (SPFile file in ExploreFolder(subFolder))
{
yield return file;
}
}
}
If I had to guess: you don't actually want to add them to a SPFileCollection. Doing this means that you're copying the files, but without using the convenient Copy method.
You probably just want to store them temporarily in a List<SPFile> or similar.
There are a lot of classes in the SharePoint object library called Collections, but they are not meant to be used like classes in the Systems.Collections namespace.

How to allow file upload for Anonymous user in SharePoint

I would like to allow anonymous user to upload files in SharePoint.
After making some research, I wrote this codes to allow anonymous user to upload. but anonymous user can't log in and is asked to log in.
Help me please. What is wrong with my codes ?
public string CreateSPFilet(string spServerURL, string tempLibrary, string folder, Stream fileStream, bool overwrite)
{
string strError = "";
string fileName = "";
try
{
if (!SPSite.Exists(new Uri(spServerURL)))
{
strError += ERROR01;
}
else
{
SPSite tempSite = new SPSite(spServerURL);
SPUserToken systoken = tempSite.SystemAccount.UserToken;
using (SPSite site = new SPSite(spServerURL, systoken))
{
SPWeb oWebsite = site.OpenWeb();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(site.ID))
{
using (SPWeb eevatedWeb = elevatedSite.OpenWeb(oWebsite.ID))
{
oWebsite.AllowUnsafeUpdates = true;
string targetUrl = SPUrlUtility.CombineUrl(oWebsite.ServerRelativeUrl, tempLibrary);
SPFolder target = oWebsite.GetFolder(targetUrl);
SPFileCollection files = target.Files;
SPFile file = target.Files.Add(fileName, fileStream, overwrite);
oWebsite.AllowUnsafeUpdates = false;
}
}
});
}
}
Within the last using statement you need to use the references to the elevated object instead of the other ones. Like so:
eevatedWeb.AllowUnsafeUpdates = true;
string targetUrl = SPUrlUtility.CombineUrl(eevatedWeb.ServerRelativeUrl, tempLibrary);
SPFolder target = eevatedWeb.GetFolder(targetUrl);
SPFileCollection files = target.Files;
SPFile file = target.Files.Add(fileName, fileStream, overwrite);
eevatedWeb.AllowUnsafeUpdates = false;
You also need to dispose of the objects tempSite and oWebSite, since they are not disposed by your above code.

Categories

Resources