简体   繁体   中英

How to programmatically change page name and url in SharePoint 2010 with EventReceiver

What I want to achieve is simple. When user create a new page in SharePoint 2010, I want to remove special character and truncate the page Name/URL to a certain number of characters.

Eg: user type in "I Want To Create This Page With An Extra Long Name !@#$%^^&**_+" in the create new page dialogbox, the actual page that get create is "extralongname.aspx"

I got the remove special character and truncate part. I just can't change the page Name/Url.

Any idea?

Regards,

Ken

It is really easy. You just have to change the value of field FileLeafRef .

using (SPSite site = new SPSite("https://sharepoint-site.domain.com"))
using (SPWeb web = site.OpenWeb())
{
    SPList list = web.Lists["Your list"];
    SPListItem item = list.GetItemById(1);

    // next row is important
    item[SPBuiltInFieldId.FileLeafRef] = "Your page url and title.aspx";
    item.Update();
}

I found your question by searching for an simliar problem.

I guess you've solved the problem in the meantime. Maybe it can help other people having the same issue.

Are you doing this with the SPSecurity Object?

eg.

SPSecurity.RunWithElevatedPrivileges(delegate(){
SPSite site = new SPSite(siteUrl); //You need the url here

   using(SPWeb web = site.OpenWeb();
   {
      web.Title = "The new Title";
      web.Update();
   }

});

It's because the user may not have the privileges doing this.

But this should do the trick!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM