Integrate Sharepoint Document List with Sitecore

One of the interesting modules in Sitecore is the Sharepoint Integration Framework (SPIF) that allows Sitecore to integrate data (View/Save) with Sharepoint. You can install the SPIF module that matches your Sitecore version from http://sdn.sitecore.net/Products/SPIF.aspx

There is two types of integration, Page Level integration in which you can use SharePoint controls (sublayouts) to display SharePoint lists inside your Sitecore site and the other type called Item Level integration in which Sitecore has the full control over the SharePoint content provided. For more information, you can refer to this manual:

Sitecore 6.3-6.5: http://sdn.sitecore.net/upload/sdn5/products/spif/spif_developers_cookbook_10-a4.pdf

Sitecore 6.3-7.0: http://sdn.sitecore.net/upload/sdn5/products/spif/11/spif_developers_cookbook_11-a4.pdf

The thing with these controls is that they have the SharePoint style (design, look and feel, etc..) so sometimes it would look a little bit odd in your Sitecore site but Sitecore always gives you the solution by using the SharePoint APIs integrated withing the SPIF module. Here is a quick way to display SharePoint lists within your site using these APIs.

Integration Pr-requisite:

  1. Download the SPIF package or at least get the integration dlls into your bin folder.
  2. Get a running SharePoint instance and integrate the connection details into the sharepoint.config file or put at any config file to be used later when creating the SharePoint context.

Integration steps:

First thing we need to define the SharePoint context. In this class, I just listed all the possible ways to read the information needed to create the context including the server name and credentials.

Once we set up this integration context, everything else is just straightforward. I modified the function above to return the server name as well since we need it when reading the SharePoint list.

You need to make sure to use the same list name that is defined in SharePoint site.

Finally, we will bind the returned list to a repeater and in this case we are returning a list of documents that will be opened inside SharePoint.

That’s it!! All you need to have your control styled the way you wish your list to look like.

Advertisements

Import Data from Excel Sheet into Sitecore

I managed to work recently on many sites that required importing Data into Sitecore from an Excel Sheet. These Data might be from Sharepoint, xml, Migrated data, actually any kind of data that you managed to store in Excel and need to import into Sitecore.

Below, a number of code snippets that allows you to do so by using CSharpJExcel.Jxl library to read Excel sheets.

Create a method to that takes the destination path (where the new Item would be stored in Sitecore tree), Item name and the template ID from which the new Item would be created.


Once the Sitecore item is created, you can loop through all the item fields to update the Sitecore Item field with the new value. This method takes a Sitecore item, Sitecore field and the new value to update the field.

The last step would be to read the Excel sheet, loop through the entire sheet and create a new Sitecore item at each row. Once an item has been created, we would update fields based on columns values. In this case, I had a dictionary that maps these column names in the Excel sheet with the Sitecore fields. Another approach would be saving data into the Excel sheet with same name of the Sitecore fields id you already know the template otherwise, you need to create a mapping file (xml for example), that directs the mapping between the Excel sheet columns and Sitecore fields.

That’s it, you can create another method to validate the Item name and put these all together in a simple import page with a button.

How to download a file from Sitecore Sheer UI Popup

I had this case where you need to download a file from a Sitecore Sheer UI Popup, like a PDF file or Excel Sheet directly from the browser without using the SaveAs dialog. The first thing that comes to your mind is to use the HttpResponse to download the stream file as the following:

You should not be surprised if no matter you tried to do so, the file will not be there!! As the popup will not allow writing the response (the response would be empty and no files are there). What I did to overcome this issue after trying different approaches and solutions is to use an IFrame and call the download code above in the page load. So what happens here is that the download button will read the file and store file information in the session (you can use any format, these used here for more clarification) and build up the IFrame and call the SheerResponse.Eval to call the IFrame page. Once all are downloaded, you can clear the session.

It took time to figure out this solution so I hope it would be helpful 🙂

Import Data from Excel Sheet using Sheer UI Dialog in Sitecore

One of the most common functionalities that you would likely work on is import from Excel file. In one of our Sitecore sites, we had a Sheer UI application that would save different kind of data that users can add manually through a popup dialog. This would be a straight forward functionality until you start to have a huge amount of data to be imported in the system and it always will end in having an excel file with all data and you need to import it to the site.

Assumptions

To have this work in a Sheer UI application in Sitecore, you have to put in mind a number of considerations:

  • There is no Sitecore control you can use directly to browse for files and upload data. The File control introduced in Sitecore is mainly used for Media Items where the Browse button will open the Media Library Tree.
  • We can always use ASP.Net controls with Sitecore. Keeping in mind how these controls work and how to implement our functionality based on that.
  • When we use ASP.Net/HTML upload control, we need first to upload the file to the server and then import data from the file. This means we need to have a post back to the server before we have the data file in hand.

Implementation

Step 1: Create XML Layout
Based on the above, we created a XAML file (.xaml.xml) to hold our File Upload control, Upload and Import buttons.

Step 2: Implement Upload Functionality
You can see that by adding the ASP.Net file upload, we used a submit button to upload the data to the server. You can implement the file upload the way you wish. Below, a sample code for doing that.

Step 3: Close Dialog and Call Import Command
Once the file is uploaded to the server, we need to:

  • Read data from the Excel sheet and Save data.
  • Close the Sheer UI dialog.

To do this, we had the import button do the following:

This will call the Sheer UI dialog OK_Click event that would close the dialog and call the command defined for saving the imported data defined in the configurations.


Step Four: Implement Save Functionality

In this step, we parsed the parameters passed from the Sheer UI application including:

  • File name including the location of the file.
  • Current user, in order to run the progress box under a Sitecore logged in user.

We used CSharpJExcel.dll library to read the Excel sheet file and through the sheets columns.

This was a simple walkthrough on how to create a XAML Popup Dialog to import data from Excel file in Sitecore.

How to remove “Insert from Template” option in Sitecore

Let’s face it! You can’t always satisfy business needs in the happy case scenario of doing things where all you need to do is one, two three and voilà everyone is happy. Specific business cases sometimes need workarounds and more line of codes to implement but they are always possible in Sitecore.

One of these cases that we were up to was to remove the “insert from template” option for a certain site in a multi-micro site environment. There are many ways of doing this, some that would affect the entire solution and some that would affect certain users, but what if I need to remove this option even from Sitecore Administrator and do it only for certain templates!

Option 1:

For the first glance, permissions would be the best and perfect option to do this where all you need to do is to remove the read permission from the insert options for certain users. To do this:

    1. Switch to the Core database.
    2. Navigate to /sitecore/content/Applications/Content Editor/Menues/New/Insert from Template item.
    3. From Security tab, select assign and the below screenshot will popup up. Revoke the read permission for any user/role you wish to.

Option1-SecuritySettins

This option will remove the “insert from template” option from the entire site for those specific users/roles and yet we didn’t solve our problem.

Option 2:

You can remove or just relocate the “insert from template” option from /sitecore/content/Applications/Content Editor/Menues/New/Insert from Template. This will serve as option 1 for all users/roles and for the entire site in a single step and having the same conclusion.

Option 3:

Use a command template to implement the same functionality of option 1. To do so you need to navigate to the template you wish to remove the “insert from template” option from and:

  1. Insert a definition item for the command template using the System/Branches/Command Template data template.
  2. In the Command field in the data section add item:addfromtemplate(id=$ParentID).
  3. Implement the command template, you can reference this for more details http://sdn.sitecore.net/upload/sitecore6/datadefinitioncookbook-a4.pdf.
  4. Finally, move, delete, or deny read access for the Everyone role in the Sitecore security domain to the “Insert from Template” item in the Core database.

As you can see that although you can do it programmatically, you still need to configure the Core database and affect the entire site. Sigh!

Are we running out of options to solve this scenario? …. What if I build how the menu looks like in Sitecore and just remove the “Insert from Template” option, is that possible?

Option 4:

Well, absolutely. You can re-build the menu item for Sitecore just by overriding the ItemNew command. “ItemNew” command is triggered whenever you need to create a new item based on another item (template). The basic idea to solve our problem is

  1. When right-click on a certain item, check whether the current item is created from the template we wish to remove the “insert from template” from.
  2. If this item fits the criteria, check if the current menu list item is the “insert from template” item.
  3. Remove the code for inserting this option.

This way, we can ensure that all users are not able to choose any other template to create items for certain sites than those assigned in the insert options. Moreover, we can apply this to any item in any level of the Sitecore tree without affecting other items or sites.

To implement this you need to:

  1. Create a class that overrides the “ItemNew” command.
  2. Override the GetSubmenuItems function with the code below by:
      1. Retrieve the insert options assigned to the current item.Option4-OvverideInserNewItem-1
      2. Check for the “insert from template” option ID and do not add the menu items.Option4-OvverideInserNewItem-2
  3. Replace the “ItemNew” command in \Website\App_Config\ Commands.config with the new one.

<command name=”item:new” type=”YourProject.InsertFromTemplate.RemoveInsertFromTemplate, YourProject ” />

And that’s it… you can insert new items without the “Insert from Template” option 🙂