简体   繁体   English

如何使用EventReceiver在SharePoint 2010中以编程方式更改页面名称和URL

[英]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. 当用户在SharePoint 2010中创建新页面时,我想删除特殊字符并将页面名称/ URL截断为一定数量的字符。

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" 例如:用户在“创建新页面”对话框中输入“我要使用超长名称创建此页面!@#$%^^&** _ +”,则实际创建的页面为“ 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 . 您只需要更改FileLeafRef字段的值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? 您是否正在使用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! 但这应该可以解决问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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