Sunday, January 22, 2012

How to remove ribbon buttons from ribbon - Sharepoint 2010

If you want to remove some buttons from sharepoint 2010 ribbon, here is a simple way to do it

Add the new item Empty Element from the vs 2010 and put this part inside Elements.xml
 


 
  
   
       
   
  
 



Here i am removing the "StartWorkflow" button from publishing tab

to get list of ribbon button locations you can see this link http://msdn.microsoft.com/en-us/library/ie/ee537543.aspx.


Monday, January 16, 2012

How to create new sharepoint user - sharepoint 2010


        private SPUser CreateUser(string strLoginName, string strEMail,string strName, string strNotes)
        {
            SPUser result = null;
            SPSite spSite = null;
            SPWeb spWeb = null;
            
            try
            {

                //Open the SharePoint site
                SPUserToken sysToken = SPContext.Current.Site.SystemAccount.UserToken;
                spSite = new SPSite(SPContext.Current.Site.RootWeb.Url, sysToken);
                using (spSite)
                {
                    spWeb = spSite.OpenWeb();

                    //Assign role and add user to site
                    spWeb.AllowUnsafeUpdates = true;
                    SPRoleAssignment spRoleAssignment = new SPRoleAssignment(strLoginName, strEMail, strName, strNotes);

                    //Using Contribute, might need high access
                    SPRoleDefinition spSPRoleDefinition = spWeb.RoleDefinitions["Contribute"];

                    spRoleAssignment.RoleDefinitionBindings.Add(spSPRoleDefinition);
                    spWeb.RoleAssignments.Add(spRoleAssignment);

                    //Update site
                    result = spWeb.AllUsers[strLoginName];
                    spWeb.Update();

                }
            }
           
            catch (Exception ex)
            {
               //do some exception handling here
            }
            finally
            {
                spWeb.AllowUnsafeUpdates = false;
                spWeb.Close();                                              
            }

            return result;
        }

Sunday, January 15, 2012

How to get user profile image - sharepoint 2010

There are many way to get user profile image.Here is a one of these methods.



private String getUserImage(String uname){
   SPUserToken userToken = SPContext.Current.Site.SystemAccount.UserToken;
   using (SPSite site = new SPSite("http://someurl.com", userToken)){
                try
                {
                    SPServiceContext serviceContext = SPServiceContext.GetContext(site);
                    Microsoft.Office.Server.UserProfiles.UserProfileManager upm = new             Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext);
                    Microsoft.Office.Server.UserProfiles.UserProfile u = upm.GetUserProfile(uname.Trim().ToString());
                    String ur = u.GetProfileValueCollection("PictureURL").ToString();
                    return ur;
                }
                catch (Exception) {
                    return "";
                }
                
            }
        }


How to get user profile in sharepoint 2010


                UserProfileManager pm = new UserProfileManager(SPServiceContext.Current);
                UserProfile currentUserProfile = pm.GetUserProfile(loginName);
                String mySiteURL= currentUserProfile.PersonalUrl.ToString();


                SPSite site = new SPSite("http://someurl.com");
                ServerContext context = ServerContext.GetContext(site);
                UserProfileManager profileManager = new UserProfileManager(context);
                UserProfile currentUserProfile = profileManager.GetUserProfile(loginName);