1

I'm working on a remove event receiver which when triggered by an ItemDeleting event should copy the list item (file) from the original document library to another.

Please see a code below for reference:

private void HandleItemDeleting(SPRemoteEventProperties properties)
{
    using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
    {
        if (clientContext != null)
        {
            var documentLibraryName = properties.ItemEventProperties.ListTitle;
            var documentLibraries = clientContext.LoadQuery(clientContext.Web.Lists.Where(l => l.Title == documentLibraryName));
            clientContext.ExecuteQuery();

            var documentLibrary = documentLibraries.FirstOrDefault();

            if (documentLibrary == null)
            {
                return;
            }

            var archiveDocumentLibraryName = properties.ItemEventProperties.ListTitle + " - Archive";
            var archiveDocumentLibraries = clientContext.LoadQuery(clientContext.Web.Lists
                .Where(l => l.Title == archiveDocumentLibraryName));
            clientContext.ExecuteQuery();

            var archiveDocumentLibrary = archiveDocumentLibraries.FirstOrDefault();

            if (archiveDocumentLibrary == null)
            {
                return;
            }

            //** insert code here to copy file (item) from documentLibrary to archiveDocumentLibrary **//
        }
    }
}

Note that the purpose of the code above is to help illustrate the problem and can be modified as long as it solves the original intent of copying the file triggering the event to another document library.

What is the best way to accomplish this?

0

There are several techniques you could use, and lots of examples on the web...

CSOM: do a web search for "csom copy file from library"

REST web services: do a web search for "SharePoint REST copy file from library"

.NET file upload classes: use the WebClient, or other classes, to upload the file to a URL.

  • Thanks for your post. Do you have any experience working with any of the "techniques" mentioned? Can share any code? – JCallico Sep 20 '18 at 14:42
  • There are many examples of each technique on the web. Here's one REST call that moves the file and its metadata: /sites/Site1/_api/web/lists/getbytitle('Documents')/items(8)/file/moveto(newurl='/sites/Site2/Documents/yourDocument.docx') – Mike Smith - MCT - MVP Sep 20 '18 at 15:44

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.