2

I'm building a console app which gets a file from a folder in sharepoint, processes it, and then moves it to a "Processed" folder on Sharepoint. I've got my app to login but having real trouble getting the document as I don't quite understand the sharepoint structure.

Do I need to use a url or go through a document library? How do I then either move the file or delete it and upload to another folder?

This is what I've tried at the moment.

many thanks guys!

1 Answer 1

0

I'm sure that this is a common scenario that you'll have no difficulty searching a bit.

Here's a small example (it's actually an image found here but I don't feel like writing into actual code :p )

public void MoveFile()
{
    ClientContext ctx = new ClientContext("http://site/");//_spPageContextInfo.webAbsoluteUrl
    Web web = ctx.Web;

    //fetch our item
    ListItem item = web.Lists.GetByTitle("My List").GetItemById(1);
    ctx.Load(item);
    ctx.ExecuteQuery();

    if(item.FileSystemObjectType == FileSystemObjectType.File)
    {
        //load up file object
        File file = item.File;
        ctx.Load(file);
        ctx.ExecuteQuery();

        //commit move
        file.MoveTo("http://site/Library2/1.docx", MoveOperations.Overwrite);
        ctx.ExecuteQuery();
    }
}

Edit:

updated screen shot to text

4
  • What do I put for the address as I've got the main address company.sharepoint.com but then I only need the /site/project? And then once in that folder it's got a document library but I've tried to get the list for that and inside that list it doesn't have the files that are actually in there? Please help, my brain hurts! Commented Sep 15, 2017 at 13:53
  • you have to input the full url of your website where the list is located. if unsure open a browser console while in the site and type _spPageContextInfo.webAbsoluteUrl Commented Sep 15, 2017 at 13:58
  • That works thanks. I can get a certain document using the getById however I need to get into a folder inside of that Documents list. How do I then do that? Commented Sep 15, 2017 at 14:16
  • if I'm not mistaken GetItemById will get any item even if it is inside folders. if you really need to get to the folder, use web.GetFolderByServerRelativeUrl Commented Sep 15, 2017 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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