Sunday, October 28, 2012

Add permission level to sharepoint web site using powershell

Using following function you can create new pemission levels using powershell





function addPermissionLevel([string]$siteurl,[string]$permlevelname,[string]$desc,[string]$baseperms){
 $web = Get-SPWeb $siteurl  
 if($web -ne $null){
  if($web.RoleDefinitions[$permlevelname] -eq $null)
  {    
   $spRoleDefinition = New-Object Microsoft.SharePoint.SPRoleDefinition
   $spRoleDefinition.Name = $permlevelname;
   $spRoleDefinition.Description = $desc;    
   # Use the command [System.Enum]::GetNames("Microsoft.SharePoint.SPBasePermissions") to get a list of possible BasePermission values    
   $spRoleDefinition.BasePermissions = $baseperms;
   $web.RoleDefinitions.Add($spRoleDefinition)
   
   $chk = ($web.RoleDefinitions[$permlevelname])
   if($chk -ne $null){
    write-debug "Site definition $permlevelname addedd."
    return $true
   }else{
    write-debug "Site definition $permlevelname not addedd."
    return $false
   }    
   $web.Dispose()
  }else{
   write-warning "Site definition $permlevelname already exists !"
   $web.Dispose()
   return $true;
  } 
 }else{
  write-warning "Site does not exists !"
  return $false;
 }
}

Follwing list is the the different types of permission parameteres

EmptyMask
ViewListItems
AddListItems
EditListItems
DeleteListItems
ApproveItems
OpenItems
ViewVersions
DeleteVersions
CancelCheckout
ManagePersonalViews
ManageLists
ViewFormPages
Open
ViewPages
AddAndCustomizePages
ApplyThemeAndBorder
ApplyStyleSheets
ViewUsageData
CreateSSCSite
ManageSubwebs
CreateGroups
ManagePermissions
BrowseDirectories
BrowseUserInfo
AddDelPrivateWebParts
UpdatePersonalWebParts
ManageWeb
UseClientIntegration
UseRemoteAPIs
ManageAlerts
CreateAlerts
EditMyUserInfo
EnumeratePermissions
FullMask

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


Tuesday, June 21, 2011

How to get current user information - sharepoint 2010

There so many ways to get the user data in sharepoint . here below shown one of the simple and eazy method.

 SPWeb web = SPControl.GetContextWeb(Context);
 SPUser sUser = web.CurrentUser;