I have been tasked with figuring out how to rename files that are coming into SharePoint. We have an External list that does not have pictures stored as blobs or even images within the SQL Database where the External List is accessing. The third party Application saves information to the SQL Database, but then saves the image on a network drive. The filepath and name is stored a column in the SQL Database.

So the external list pulls the filepath field into a column in SharePoint. We have a custom action that then opens the filepath. At this point files are stored in site assets in SharePoint with the same file names. Unfortunately, the Query Parameter on the end of the url http://intranet/staffdirectory/siteassets/{0} The query puts a + where the spaces are. The application unfortunately has no way of removing the spaces when a file is saved. So when a user clicks on the Open Image Action. they are returned an error since the URL has + instead of %20!

Phew, if that made sense now you understand why I need a timerjob to go in and clear out the spaces within SharePoint. I have no idea how to use SPObjects and this is where i am stuck.

public class CustomTimerJob : SPJobDefinition
    public CustomTimerJob() : base() { }
    public CustomTimerJob(string jobName, SPService service) :
        base(jobName, service, null, SPJobLockType.None)
    { this.Title = "Rename Files Timer"; }
    public CustomTimerJob(string jobName, SPWebApplication webapp) :
        base(jobName, webapp, null, SPJobLockType.ContentDatabase)
    { this.Title = "Rename Files Timer"; }

public override void Execute(Guid targetInstanceId)
    SPWebApplication webApp = this.Parent as SPWebApplication;
    SPList siteAssets = webApp.Sites[0]


How do I know which Site Collection Index is the right location? How do I iterate through each item in the list? Can I give each file that has been named a boolean value so the TimerJob doesn't check every single one each time?

  • Sorry, Edited for continuity Commented May 22, 2015 at 14:48

1 Answer 1


You can create an SPSite Object using URL

using(SPSite site = new SPSite("http://intranet/staffdirectory")) {
    using(SPWeb web = site.OpenWeb()) {
        SPList list = web.Lists["SiteAssets"];
        SPListItems items = list.Items;
        foreach(SPListItem item in items) {
            //Logic to rename file
            item["Name"] = item["Name"].ToString().Replace("+","%20");            
            item.File.CheckIn("file name has been changed");
  • The files in Site Assets have spaces. It's the Query Parameter that the Custom Action on the External list that is putting the "+" would ToString().Replace(" ", ""); work? Commented May 22, 2015 at 15:11
  • Additionally this timerjob will run on every single item every time. Is it possible to give a boolean value that changes, or would it be better to have the code check if item.file.checkin contents be ("file name has been changed")? Commented May 22, 2015 at 15:14
  • You can add a new column to the SiteAssets List and set it after renaming. For further optimization you can retrieve only not renamed items instead of retrieving all items. Commented May 22, 2015 at 15:15
  • Okay, that should be enough to get me started. Is that supposed to be SPListItemCollection? And do I need to do anything else to the timerjob after creating a feature? It is scoped to the web application level Commented May 22, 2015 at 15:23

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.