Domanda

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?

È stato utile?

Soluzione

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.File.CheckOut();
            item["Name"] = item["Name"].ToString().Replace("+","%20");            
            item.Update();
            item.File.CheckIn("file name has been changed");
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top