Max Zhukov
Max Zhukov

Reputation: 897

Programmatically add webpart to User Profile page

I'm trying to add webparts to user profile page programatically. I have this code:

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();
                    }
                }
            }

But it throws exception on creating SPLimitedWebPartManager: The file http://localhost/layouts/userdisp.aspx?force=true&id=1 does not exist.

When I try to copy and paste this url to browser, it works fine. What is the trouble? Thank you!

Upvotes: 1

Views: 495

Answers (1)

Yevgeniy.Chernobrivets
Yevgeniy.Chernobrivets

Reputation: 3194

You cannot edit application pages (pages which are located in _layouts virtual directory).

The only solution which I can see is to configure user profile service. After doing that /_layouts/userdisp.aspx will no longer be used to display user profiles and you will be redirected to "My Sites" user profile page which you can edit.

EDIT: If i'm not mistaken you need to configure "My Sites" as well.

Upvotes: 1

Related Questions