Tuesday, September 27, 2011

Run With Elevated Privileges



Say you have implemented a webpart in one of your sharepoint sites. This web part will be used by all types of users from admins to readers. If some part of your web part code need to execute with Full Control rights even if the user does not have Full Control, SPSecurity.RunWithElevatedPrivileges Method can be used.


The following example sends an email using elevated privileges. This will elevate the privileges to execute using the service account. The SPSecurity.RunWithElevatedPrivileges method taks a delegate method as its argument and executes that code with the service account.


public void SendEmail(Email email)
{
   SPSecurity.RunWithElevatedPrivileges(delegate()
   {
        SmtpClient mail = new SmtpClient();    MailMessage message = new MailMessage();
            message.From = new MailAddress(this.SenderAddress);
            message.To.Add(email.To);
            message.IsBodyHtml = email.IsHtml;  
            message.CC.Add(email.Cc);  
            message.Bcc.Add(email.Bcc);  
            message.ReplyTo = new MailAddress(this.ReplyToAddress);  
            message.Subject = email.Subject;  
            message.Body = email.Body;


        mail.Host = this.CentralAdminOutboundServerAddress; mail.Send(message);
    });
}


This would allow the lower users too to use the email functionality without any issues even though it uses mail settings from central admin.



Delegate method or delegated block of code being excuted within RunWithElevatedPrivileges, will be executed with privileges of System Account i.e. the App Pool identity of the respective web application.
RunWithElevatedPrivileges should be used when code should execute SharePoint API on securable objects or processes for which current user doesn't have pemissions.
If it is required to access SPSite or SPWeb object for the current site, while executing block of code within RunWithElevatedPrivileges, new context for the SPSite and SPWeb should be created.
Common mistake developers make is use of SPSite or SPWeb objects retrieved from SPContext.Current in RunWithElevatedPrivileges block. This objects will have current user permission levels on the securable objects, not of the System Account.

Saturday, September 24, 2011

To remove button from Ribbon

Following are the steps to remove this button from Ribbon


In Visual Studio 2010:
  1. Create an empty SharePoint 2010 Solution , you may give your favorite name to it
  2. Add a feature by right clicking on the Features in Solution Explorer
  3. Right click on the Project and add a new item, choose empty element and name it
  4. Open the Elements.xml file from the newly added item
  5. Copy & paste the following XML into the Elements.xml file
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction
     Id="RemoveRibbonButton"
     Location="CommandUI.Ribbon">
       <CommandUIExtension>
           <CommandUIDefinitions>
              <CommandUIDefinition
               Location="Ribbon.Library.Actions.OpenWithExplorer" />
           </CommandUIDefinitions>
       </CommandUIExtension>
    </CustomAction>
</Elements>

Deploy the Solution and observe that “Open with Explorer” button will be removed from the Ribbon.


Without Visual Studio: With SharePoint 2010 Management Shell (PowerShell)

  • Create a folder under %Program Files%\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\RemoveRibbonButton
  • Create a feature.xml file in RemoveRibbonButton.

<?xml version="1.0" encoding="utf-8" ?>

 <Feature Id="MyFeatureId"
 Title="Remove button feature"
 Description="Description of the Feature." 
 Version="1.0.0.0" 
 Scope="Web" 
 xmlns="http://schemas.microsoft.com/sharepoint/">
 <ElementManifests>
  <ElementManifest Location="manifest.xml" />   
  </ElementManifests>
 </Feature>


  • Create a blank xml file - manifest.xml in RemoveRibbonButton folder and add the below content to it and Save it.


<?xml version="1.0" encoding="utf-8"?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction
     Id="RemoveRibbonButton"
     Location="CommandUI.Ribbon">
       <CommandUIExtension>
           <CommandUIDefinitions>
              <CommandUIDefinition
Location="Ribbon.Library.Actions.OpenWithExplorer" />
           </CommandUIDefinitions>
       </CommandUIExtension>
    </CustomAction>
</Elements>

  • Deploy the customization using SharePoint 2010 Management Shell (or Windows PowerShell): Install-SPFeature MyFeatureId Enable-SPFeature FeatureId -Url http://server/site/subsite (replace http://server/site/subsite on your site url)



Monday, September 19, 2011

FBA , Claims Auth in SP 2010


You may use the Forms Based Authentication for this. Here the credentials would be stored in an external SQL DB or similar data store.


Configure site in Claims Authentication Mode and configure membership and role provider details. This will give you default login page. You can customize it per your branding


You can create additional web parts to add user, remove, edit etc which would eventually write data to the SQL database used by FBA.


Following links would be helpful


http://blogs.msdn.com/b/kaevans/archive/2010/07/09/creating-a-custom-login-page-for-sharepoint-2010.aspx 


http://blogs.msdn.com/b/brporter/archive/2010/05/10/temp.aspx


http://www.topsharepoint.com/enable-anonymous-access-in-sharepoint-2010

Friday, September 16, 2011

To find the user name of a Manager of an user programatically




You can use spUser object to find the required user from Users collection of a site.
Instantiate a UserProfileManager object and assign the UserProfile by executing profileManager.GetUserProfile method.
The profile collection has a tag "Manager", retrieving that Value will give you the result.

Thursday, September 15, 2011

Tech jobs in SharePoint world

Quick note on Tech jobs in SharePoint world
SharePoint Architect : Who will architect and design Sharepoint farms and solutions for clients
SharePoint Developer : Who implements the sharepoint solution using out of the box features, configured features (using SharePoint Designer and SharePoint Web UI)  and develops .Net based code to accomplish certain features that are not available in SharePoint native feature set.
SharePoint Admin : Who administers the sharepoint environment and monitors the process, logs, health, content growth, backup etc etc 

Thursday, September 8, 2011

Quick and easy way to fix master page issue after creating site from a custom template


At times the sites created from customm site templates may not looks properly. This could be due to the master page not applied correctly.


Below the Quick and easy way to fix it 


To activate publishing features

  • First enable publishing infrastructure feature at site collection level
  • then activate SharePoint Server publishing feature at site level.

To apply master page

  • Open your source web app in sharepoint designer
  • select the master page from master pages gallery
  • Choose Export File option to save master page as file
  • Open your destination web app in sharepoint designer
  • Import the saved master page into its gallery
  • Set the newly imported master page as site master page

Tuesday, September 6, 2011

SharePoint Features


Per Microsoft, SharePoint feature is a modular server-side, file system-level customization (code based solution) that contains items that can be installed and activated in a SharePoint environment.


Say I need an additional functionality to be implemented on my sharepoint site, we use to create a feature for that and install and activate at the right level (farm, web app , site collection or site) to get to done properly.


Advantages of feature are


Features would help add / remove the functional components together as a single entity which would help the management easier
Feature would allow owners / admis of a site to turn on / off the features as per the reuirement


Technically Features is a collection of one or more feature.xml file that is located in a custom subdirectory of the features directory. Features can also contain other files that make up its components, such as and most commonly an XML file that contains a list of Feature elements. 

Saturday, September 3, 2011

Workflow Basics


SP Workflow is an option to manage business process generally oriented around SharePoint list data model. User can start SharePoint workflow manually but they usually start automatically in response to some event connected with a list item or library item such as adding, deleting or updating an item. 

There are workflows associated with subsites rather than lists or content types - these cannot be started in response to an event ; it  has to happen manually or programatically. Such workflows will be helpful in setting and retrieving multiple list items across lists or performing non list operations like creating and configuring subsites.

The Reusable and Globally Reusable workflows in SPD is another kind of list workflow that can be used on multiple lists without having to recreate them for for each list.

SharePoint workflows are built on top of WF. when a workflow is running in SharePoint WF engine is hosted in SP Process; it loads and unloads WF templates and provides sequencing and persistence for workflows. WF enables customization in transaction, persistence, notifications, roles, tracking and messaging of SharePoint workflows.   
Every SharePoint Workflow is represented by the following Two entities that work together

  1. Workflow Definition that specifies the paremeters, events (like activation), sequence of activities , branching structure, conditions that determine the paths of execution. WF Definition can be defined in code and compiled into an assembly. The other option is to define it in  an XOML markup (declarative / nocode workflow) where it persist uncompiled in content DB until its called
  2. Workflow Template Definition which is an XML file that contains the info require to instantiate and run workflow like name of WF, Description, class with WF assembly to call, Identity of WF assembly, Location of any custom forms. These XML files can be stored at 14\TEMPLATE\1033\Workflow or Workflows list of root site in a site collection. In rutime, WF template defintion is used to create a cached SPWorkflowTemplate object.

The extensibilty options in WF are

  1. Custom compiled workflows using VS Workflow designer
  2. Custom Declarative WF using SPD

Usually Workflows installed as Site Collection Level Features but we can also publish WF directly from SPD which doesnt require any feature. Every WF association will have an entry in Site Collection Content DB.