Currently I'm looping through all the files in a collection, which is every single document in the library. Once I find the unique document name, I move it.

I'm trying to getting away from looping over thousands of records.

Maybe i can write a CAML query that will reduce the files in the collection maybe?

    SPFileCollection collFiles = site.GetFolder("Final Candidates").Files;
    for (int intIndex = collFiles.Count - 1; intIndex > -1; intIndex--)
      if (collFiles[intIndex].Name == CName)
        collFiles[intIndex].MoveTo("Draft Candidates/" + CName, true);

I figured it out.

You can use SPFile to check if the file exist in the library and then move it if it does.

SPFile file = site.GetFile("/Final Candidates/" + CName);

if (file.Exists)
   file.MoveTo("Draft Candidates/" + CName, true);

Reference: https://stackoverflow.com/questions/359672/how-to-determine-if-a-file-exists-in-a-sharepoint-spfolder

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.