Search code examples
c#sharepointevent-receiversharepoint-feature

How to copy a text file from a module to a document library that is not created, in SharePoint event receiver?


I have a text file in module and I need to copy that file to a document library that is not existed yet, and it is going to be created in featureActivated. What should I do in this situation?


Solution

  • Deploy to an existing library and then calling MoveTo in event receiver.

    Sample demo:

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Module Name="MyModule" Url="MyDOc">
        <File Path="MyModule\Sample.txt" Url="Sample.txt" Type="GhostableInLibrary"/>  
      </Module>
    </Elements>
    
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {           
                SPFile file = site.OpenWeb().GetFile("MyDOc/Sample.txt");
                file.MoveTo("/Shared%20Documents/Sample.txt");
    
            }