【问题标题】:Programmatically add webpart to User Profile page以编程方式将 webpart 添加到用户配置文件页面
【发布时间】:2014-06-27 15:29:04
【问题描述】:

我正在尝试以编程方式将 webpart 添加到用户个人资料页面。我有这个代码:

using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    try
                    {
                        web.AllowUnsafeUpdates = true;
                        string userPageUrl = web.Url + "/layouts/userdisp.aspx?force=true&id=" + web.CurrentUser.ID.ToString();
                        SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(userPageUrl, PersonalizationScope.Shared);
                        ContentEditorWebPart cewp = new ContentEditorWebPart();
                        cewp.ID = "test";
                        cewp.Title = "test User Profile webpart title";
                        cewp.Description = "test User Profile description";
                        cewp.Content.InnerText = "USER INFORMATION";
                        manager.AddWebPart(cewp, "Main", 1);
                        manager.SaveChanges(cewp);
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                    }
                }
            }

但是在创建SPLimitedWebPartManager:The file http://localhost/layouts/userdisp.aspx?force=true&id=1 does not exist.时会抛出异常

当我尝试将此网址复制并粘贴到浏览器时,它工作正常。有什么问题?谢谢!

【问题讨论】:

    标签: c# sharepoint-2010 web-parts


    【解决方案1】:

    您不能编辑应用程序页面(位于_layouts 虚拟目录中的页面)。

    我能看到的唯一解决方案是configure user profile service。完成此操作后,/_layouts/userdisp.aspx 将不再用于显示用户配置文件,您将被重定向到您可以编辑的“我的站点”用户配置文件页面。

    编辑:如果我没记错的话,你也需要configure "My Sites"

    【讨论】:

    猜你喜欢
    • 2010-11-28
    • 2010-09-16
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多