Showing posts with label C# coding. Show all posts
Showing posts with label C# coding. Show all posts

Tuesday, February 28, 2012

Get and Renaming SPFolder c#

This is how you can rename a SPFolder. There is no need to use MoveTo() as suggested by others.
The user has to have rights to do it, or enclose the whole thing inside a

var siteId = "Guid of my site";
var webUrl= "Server relative path to my web";
SPSecurity.RunWithElevatedPrivileges(delegate(){
    using (SPSite elevatedSite = new SPSite(siteId))
   {
       using (SPWeb elevatedWeb = elevatedSite.OpenWeb(webUrl))
      {

          string requestFolderUrl = webUrl+ "/MYLIST/OldFolderName";
          SPFolder requestFolder = elevatedWeb.GetFolder(requestFolderUrl);

          if (requestFolder.Exists)
          {

               requestFolderItem[SPBuiltInFieldId.Title] = "NewName"; //not really needed for renaming
               //requestFolderItem[
SPBuiltInFieldId.BaseName] =  "NewName"; //this Fails.don't know why
               requestFolderItem["BaseName"] =  "NewName"; //this Works!
               //don't want to update the Modified time nor the modified by Info
              //If you want to modify this info use Update, and avoid being in a RunWithElevatedPrivileges
             ///because else you would have the System User registered
               requestFolderItem.SystemUpdate(false);

           }
      }
   }
}

Thursday, March 31, 2011

Sharepoint 2010 Creating Colleagues (bidirectional) by code

This is a sample how impersonating the System Account a  two way colleagues relationship can be established by code:

Thanks to the replies of Tony for  error-when-trying-to-add-a-colleague-to-a-user-profile
Problem was that I actually got an error if I run his code with elevetad privilege, so slightly modified his solution.



using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;

namespace SharePointProject1.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void AddUser_Click(object sender, EventArgs e)
        {
                using (SPSite siteCol = new SPSite("MySiteUrl"))
                {
//Open the Site with the SystemAccount
                    using (SPSite siteAdmin = new SPSite(siteCol.ID,siteCol.SystemAccount.UserToken))
                    {
                        try
                        {
//This is really needed, else you get an exception
                           siteAdmin.AllowUnsafeUpdates = true;
                            using (SPWeb spwebAdmin = siteAdmin.OpenWeb())
                            {
                               //I didn't test if this 3 line are really needed
                                SPUser inviter = spwebAdmin.EnsureUser(@"Domain\User1");
                                SPUser invited = spwebAdmin.EnsureUser(@"Domain\User2");
                                spwebAdmin.Update();
                                SPServiceContext ctx = SPServiceContext.GetContext(siteAdmin);

                                //Get the user'sprofilemanager from the current context and ignore privacy
                                UserProfileManager upcm = new UserProfileManager(ctx, true);
                                //Gettheuserprofile
                                UserProfile userInvitee = upcm.GetUserProfile(@"Domain \User1");
                                UserProfile userInviter = upcm.GetUserProfile(@"Domain \User2");
                                //updateinviter
                                if (!userInviter.Colleagues.IsColleague(userInvitee.ID))
                                {

                                    Colleague newColleague = userInviter.Colleagues.Create(userInvitee,ColleagueGroupType.General,"General", false,
Privacy.Public);
                                    newColleague.Commit();
                                    userInviter.Commit();
                                }
                                //updateinvitee
                                if (!userInvitee.Colleagues.IsColleague(userInviter.ID))
                                {
                                    Colleague newColleague = userInvitee.Colleagues.Create(userInviter,ColleagueGroupType.General,"General", false,Privacy.Public);
                                    newColleague.Commit();
                                    userInvitee.Commit();
                                }

                            }
                        }
                        finally
                        {
                            siteAdmin.AllowUnsafeUpdates = false;
                        }
                    }
                }
        }

    }
}


.Hope it works for you guys too