Used SPFile and SPFileCollection for rename file name in Sharepoint 2010

November 16, 2010


Hi all. Today I should talk about automatic rename file name on document library in Sharepoint 2010. Sample, I have one document library and include more folders with documents inside. I can create event receiver for automatic update name file on folders in library.

I’m used class SPFile and method GetFile() for gets properties created file.

SPFile – Represents a file in a SharePoint Web site that can be a Web Part Page, an item in a document library, or a file in a folder.

This sample automatic update file name in folders document library Sharepoint 2010.

public class EventReceiver1 : SPItemEventReceiver
    {
       /// <summary>
       /// An item was added.
       /// </summary>
       public override void ItemAdded(SPItemEventProperties properties)
       {
           base.ItemAdded(properties);

           SPFile file = properties.Web.GetFile(properties.ListItem.Url);
           if (file.Item.ContentType.Name == "Document")
           {
               file.Item["Name"] = properties.AfterUrl.Replace("/", "-");
               file.Item.Update();
           }
       }

}

Next sample I can automatic update file name, when I updated name folder on my library documents.

I been used class SPFileCollection and method GetFolder for gets properties updated folder on Sharepoint 2010.

SPFileCollection – Represents a collection of SPFile objects.

Use the Files property of either the SPWeb or SPFolder class to return the collection of files for the site or folder. To create a new file, use one of the Add methods of SPFileCollection.

Use an indexer to return a single file from the collection. For example, assuming the collection is assigned to a variable named collFiles, use collFiles[index] in C#, or collFiles(index) in Visual Basic, where index is either the index number of the file in the collection or the display name of the file

This sample automatic update file name, when updated name folder document library Sharepoint 2010.

       /// <summary>
      /// An item was updated
      /// </summary>
      public override void ItemUpdated(SPItemEventProperties properties)
      {
          base.ItemUpdated(properties);

          SPFile file = properties.Web.GetFile(properties.ListItem.Url);
          if (file.Item.ContentType.Name == "Folder")
          {

              SPFileCollection fileCollection = properties.Web.GetFolder(properties.ListItem.Url).Files;

              foreach (SPFile fileItem in fileCollection)
              {
                  fileItem.Item["Name"] = fileItem.Item.Name + properties.AfterUrl.Replace("/", "-");
                  file.Item.Update();
              }
          }

      }

That’s all. If you can automatic update name folder on library Sharepoint 2010, else you can write this code:

SPFolder.Name = "NewFolderName";

SPFolder.MoveTo("brandspakningnewSPFolder.Name");

Advertisements

8 Responses to “Used SPFile and SPFileCollection for rename file name in Sharepoint 2010”

  1. Hélder Costa Says:

    Hello Belskiy,

    I want to rename a file and I’m using the following code:

    file.Item[“Name”] = “xpto”;
    file.Item.Update()

    The process stops when the Update method is called and after some time it launchs an SPException with message:

    “0x81020089A solicitação de execução de código em modo seguro foi recusada porque o Serviço de Host do Código em Modo Seguro estava muito ocupado para lidar com a solicitação.”

    This is Portuguese… translated to English:

    “The request for execution of code in safe mode was refused because the Service Host Code in Safe Mode was too busy to handle the request.”

    The application was deployed as sandbox on sharepoint.
    Do you had this exeption or have idea way this is happening?

    Regards,
    Hélder Costa

  2. belskiy Says:

    Hello Hélder Costa.
    You can restart service Service Host Code on Sharepoint Admin Center and You can deploy your code.

  3. Mike Says:

    Hello, Sergey.
    Do you know how to rename a folder in Sharepoint list?

    • belskiy Says:

      Yes,
      SPList list = web.Lists[“test”];
      SPListItem f = list.Folders[0];

      f[“Name”] = “New Name”;
      f.Update();

      • Mike Says:

        Thank you. But it says:
        The column ‘Name’ does not exist. It may have been deleted by another user.

        I tried to MoveTo. It works.

        SPListItem f= list.Folders.GetItemById(i);
        f.Folder.MoveTo(f.Folder.ParentFolder + “/newName”);

        Do you think is the right way?

  4. Millzy Says:

    Can you change the file extension with the name change, I cannot seem to get that to change?

  5. mini storage Says:

    I believe that is among the most significant information for me.
    And i’m satisfied reading your article. However should remark on some normal issues, The web site taste is perfect, the articles is truly excellent :
    D. Excellent job, cheers


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: