Subscribe:

Labels

Sunday, December 11, 2011

Create a mysite(personal site) Using the Object Model

Below code is using object model creating the my sites dynamically.


            SPSite site = new SPSite("http://win-rgqt3bch3nk:22929/sites/Test");

            SPWeb web = site.OpenWeb();
            // SPWeb web = SPContext.Current.Web;


            web.AllowUnsafeUpdates = true;

         
            if (ApproveitemId > 0)
            {
                SPList thislist = web.Lists["Requests"];
                SPListItem item = thislist.Items.GetItemById(ApproveitemId);
                string AStatus = item["Approval_Status"].ToString();
                string strYes = "Yes";
                if (AStatus == strYes)
                {
                    string mysiteusername = item["Username"].ToString();
                     //SPUser mysiteuname = web.AllUsers[mysiteusername];

                    SPSecurity.RunWithElevatedPrivileges(delegate()
                         {
                             using (SPSite spSite = new SPSite("http://win-rgqt3bch3nk:38562"))
                             {
                                 spSite.AllowUnsafeUpdates = true;
                                 SPContext.Current.Web.AllowUnsafeUpdates = true;
                                 SPServiceContext siteContext = SPServiceContext.GetContext(spSite);
                                 //UserProfileManager up = new UserProfileManager(siteContext,true);
                                 UserProfileManager up = new UserProfileManager(siteContext);
                                 //string sAccount = "WIN-RGQT3BCH3NK\\test1";
                                 //UserProfile uprof = up.GetUserProfile(sAccount.Trim());
                                 UserProfile uprof = up.GetUserProfile(mysiteusername);
                                 uprof.CreatePersonalSite();
                             }
                         });
                }
            }
         
        }

No comments:

Post a Comment