Friday, 16 September 2011

Developing SharePoint 2007 Webparts using Visual Studio 2010

Developing SharePoint 2007 Webparts using Visual Studio 2010

Today I had a client that came to me that liked a web part that I had developed and wanted me to install it for them with some slight mods. No problem… Ohh wait, we developed that for SharePoint 2010 using Visual Studio 2010.
I am a huge fan of Visual Studio 2010 and its tight integration with SharePoint 2010 in its ability to quickly create features and write code like its supposed to be written (without the major headache!). Although WSPbuilder was the tool of chose for SP 2007, VS 2010 is now my new favourite weapon. With new functions like creating Visual Web Parts in a couple of clicks, features, deploy & debug with F5,  control over deployment and much more. Its fantastic, well done to Microsoft for making what should be a simple job easy again!
Since the release of SharePoint 2010 I now do a lot my development in SharePoint 2010 even if my client is running on SharePoint 2007. Why? Well because the API’s for SP 2007 & 2010 are very similar, and really haven’t effected much of my development to date. I find it so much easier to development in SP 2010 with Visual Studio 2010 especially when I run Windows 7 64-bit with SharePoint Foundation 2010 installed. Having this setup means that i can deploy directly from my local PC to a SharePoint environment for basic testing. No need to manually create project structure, manifiest files, signing of assembly, building wsps, manually deploying, attach to debug etc. Once I’m all done with development I package it up and deploy the wsp to the Sharepoint 2007 development for thorough testing. Provided I haven’t used any deprecated methods in the API, ill be all sweet. Good thing is if you have used and deprecated methods you wont be able to build solution, so you wont be doubt when deploying to SP 2007.

Problem.

So i presume your reading this because you love the new Visual Studio 2010 but need to develop web parts for SharePoint 2007. Although the interface within Visual Studio doesn’t directly enable you to develop for 2007, it is possible with some minor changes to your project.
So for the purposes of this article I am going create a basic visual web part, something that was difficult to do in SharePoint 2007 development. This process should work for most project types available…
  1. Start New Project, select Visual Web Part
    image
  2. Select – Deploy as a farm solution
    image
  3. Write your custom web part. I have just created a simple web part that contains some text and a button. When the user clicks the button the button text will change.
    image
  4. I now press F5 on the keyboard and Visual Studio will automatically build & deploy my web part to SharePoint 2010.
  5. I confirm it all works by adding the web part to the page. Great! But there is nothing special so far.
    image
  6. Now to get it into SharePoint 2007! First lets try deploying the current wsp as it is…
  7. So we need to package up the solution.
    image
  8. Grab the wsp from the bin folder
  9. Copy to SharePoint 2007 server.
  10. Deploy to 2007 env and we get an error. The error tells us that it found a version number in the manifest that it doesnt understand.
    image “Solution manifest for solution ’48cae4c9-68ba-cccf5a89304′ failed validation, file manifest.xml, line 2, character 110: The ‘SharePointProductVersion’ attribute is not declared.

Solution

After the steps above:
  1. Go back into Visual Studio 2010.
  2. Double click the “Package.package” file
  3. Select “Manifest” down the bottom. Notice where is reads SharePointProductVersion=”14.0” in the xml file.
    image
  4. In the properties panel, clear out 14.0 in the SharePointProductVersion field. This removes the property all together from the manifest file.
    image
  5. We also need to change the assembly reference in the project from Microsoft.SharePoint.dll 14.0 to 12.0.
    To do this you will need to click the show all files icon in the solution explorer panel.  image  -Next, delete out Microsoft.SharePoint & delete out Microsoft.SharePoint.Security as these wont be on the SP 2007 environment.-Then you want to add a reference to Microsoft.SharePoint.dll but make sure its the 12.0 version found in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll
  6. Because we have built a ‘Visual Web Part’, Visual Studio 2010 automattically adds some imports into the ascx user control file which wont exists in SP 2007. Therefore you need to comment these references out. Obviously if we were doing a coded web part or a event receiver we would not need to do this step.Mine currently looks like this.
    image
  7. Package up your solution again and deploy on SP 2007 environment.
    image
  8. Active, test and your good to go.
    SharePoint 2007 webpart
Notes:
  • You will need Asp.Net 3.5 installed on your SharePoint 2007 environment.
  • This may not always be the right solution for you (depending on your project) but it certainly saves me a lot of time for those basic web parts

Wednesday, 10 August 2011

Reporting Services 2008 R2 in SharePoint 2010

              
Configuring Reporting Services 2008 R2 in SharePoint 2010
Integrating Reporting Services with SharePoint is really a fun and it gives extremely user-friendly environment for Reporting Services. There are mainly two ways of integrating Reporting Services with SharePoint. If Reporting Services is installed in Native Mode, Report Explorer and Report Viewer web parts can be used for interacting with reports. When the Reporting Services is installed in SharePoint Integrated Mode, SharePoint takes overall functionalities of Reporting Services, including SharePoint features such as versioning, alerts, and enterprise search. I recently configured SQL Server 2008 R2 in SharePoint 2010 as Integrated Mode, thought to share the experience; the reason for this post. Note that this is the way I configured the integration, you may take different approach for this integration.
I started this with Windows 2008 R2 and configured SharePoint 2010 on it. See this post “Configuring Windows 2008 R2 for SharePoint 2010” if you come across any issue with the installation. When SQL Server Reporting Services is installed, make sure that you select either “Install the SharePoint integrated mode default configuration” or “Install, but do not configure the report server”.
If you need to complete the SharePoint farm installation using non-domain accounts, this post “Complete Farm SharePoint 2010 installation on Single Server using non-domain accounts” shows you the way.
Once the server is ready with SharePoint 2010 and SQL Server 2008 R2, configuration can be started. If you have selected “Install, but do not configure the report server”, you need to configure Reporting Services in SharePoint 2010 mode. Here are steps for configuring it;
  • Open Reporting Services Configuration Manager and connect with Reporting Services instance.
  • Make sure that Report Service status is Started. Notice that Report Server Mode is Native.
  • Open the “Report Service Database” section by clicking the Database navigational tab button. It shows the SQL Server Name, Database Name, and Mode if configured. Click on Change Database button to launch Report Server Database Configuration Wizard. Here is a screenshot of it;
  • Click Next and open the next window of the wizard. Configure the database server which will host the Reporting Services instance. Once authentication is set, click Test Connection and see whether connection can be established successfully. Click Next.
  • Type the database name as ReportServer which is the default name. However you can give a names like ReportServerSharePointIntegrated too. Select the option Report Server Mode as SharePoint Integrated. Click Next to continue.
  • Set credentials that will be used for connecting to the database. If everything is in one box, Service Credentials would be enough.
  • When you click on Next, you will see the summary of what you have entered/selected. Continue with the wizard and complete it.
  • If need to change the port of Web Service URL, go to Web Service URL section by clicking Web Service URL navigational tab button and set it. I have set the port as 20000 and my Web Service URL is http://DP03:20000/ReportServer.
Next step is downloading Reporting Services Add-in for SharePoint 2010. Here is the link for downloading it:http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=b3bebf9d-d86d-48cd-94e2-0639a846be80.
Once it is downloaded, start installing it. Starting screen would be this;
Complete the installation. Now all required parts are ready. Next is configuring SharePoint for Reporting Services. Open SharePoint 2010 Central Administration and click on General Application Settings link.
Once the General Application Settings is open, a section called Reporting Services can be seen which allows managing integrating settings.
The link Reporting Services Integration opens the window where Report Server Web Service URL and Authentication Mode can be set. The Report Server Web Service URL should be the URL that has been configured with Web Service URL navigational tab of Reporting Services Configuration Manager. Authentication mode can be either Windows Authentication or Trusted Authentication. Since everything is in one box, Windows Authentication would be fine with this.
In order to user Trusted Authentication, a trusted account has to be predefined. When Trusted Authentication is used, Report Server has no knowledge on the user connected to the SharePoint. Trusted account impersonates connected SharePoint user and will be used for accessing the Report Server. Trusted Authentication is usually used with Form Authentication or windows authentication without AD or Kerberos. Here is my screen; Set Server Defaults under Reporting Services allows changing default server settings for Reporting Services. Some of this elements can be changed are; Number of Snapshots, Ad-Hoc Reporting and Client-Side Printing.
Now the integration is done. The SharePoint is fully integrated with Reporting Services. We need a hosting environment for Reporting Services. Since Reporting Services Add-In has added necessary content types for the Farm, reports can be added to any site. Or else, a new site can be created for hosting Reporting Services reports and connections. See Part II for creating a site, configuring libraries and content type, and publishing reports from BIDS.
PART II
If you need to create an extremely user-friendly environment for Reporting Services 2008 R2, SharePoint 2010 Integrated Mode is the best solution. Configuring Reporting Services 2008 R2 in SharePoint 2010 Integrated Mode – Part 1 explains the initial configuration up to SharePoint integration. This post discusses creating a web site for Reporting Services with relevant libraries and content types, finally publishing reporting from BIDS.
Let’s start creating a Web Application. Go to SharePoint Central Administration and click Manage Web Applications under Application Management. Once Application Management is open, click on New to create a new web application.
Some of the attributed in New Web Application Form as as follows;
  • Authentication
    Select Classic Mode Authentication
  • IIS Web Site settings
    Select Create a new IIS web site option. I have named it as Report Server – 10005 and set the port as 10005.
  • Security Configuration
    Leave default values
  • Public URL
    In my case, URL is
    http://DP03:10005
  • Application Pool
    Make sure Create new application pool is selected. I have named it as ReportServer – 10005.
  • Database Name and Authentication
    Set the SQL Server name for Database Server. I have named the database as WSS_Content_ReportServer_10005.
Click OK to save setting. Once the process is completed, Application Created windows is appeared. Click OK to continue. New web application will be listed in Web Application window.  
Next step is creating a site collection. Go back to Central Administration and click create site collections under Application Management. This opens Create Site Collection window. Let’s ceate one;
  • Make sure that correct web application is selected (In my case, it is http://DP03:10005/.
  • Add Title as Report Server.
  • Let’s use the root for the Web Site Address. Do not select sites from the drop-down. Leave it blank.
  • Select the Template as Team Site.
  • Set both Primary Site Collection Administrator and Secondary Site Collection Administrator. Since I have everything in one machine, I set both as DP03\Administrator;.
  • Click OK to create the site.
Once done, a message will be appeared saying Top-Level Site Successfully Created. Click on the link to go the site (In my case, link is http://dp03:10005). This is what I see;
Site needs libraries for holding reports and connections. Let’s create two libraries for the site.
  • Click on Libraries link in left pane. It lists all document libraries available.
  • Click the button Create for creating a new library.
  • Select Document Library and Set the name as Reports.
  • Click Create for creating the library. Once the library is open, click on Library Settings for adding content types.
     
  • When Library Settings is open, library related things such as General Settings, Columns, and Views are shown. By default, it does not show Content Type. In order to enable content type, click Advanced Settings under General Settings. Once the window is open, select Yes for Allow management of content types.
  • Click OK to save settings. Note that now the Content Type is shown under Library Settings. Click Add from existing site content types link under Content Type. This opens Add Content Types page.
  • Select Report Server Content Type from Content Types From drop-down. Then move Report Builder Report from left listbox to right listbox.
    Note that if you are not seeing “Report Server Content Types”, that indicates that Add-In has not been properly installed”.
  • Click OK to save settings. Report Builder Report content type is now appeared under Content Types of Reports library.
  • We have to create another library for data connection. Follow same steps for creating the library. Name it as Data Connections. Then enable Allow management of content types as did for Reports library. Then add Report Data Source content type for it.
Now SharePoint 2010 is ready for holding reports from Reporting Services 2008 R2. Let’s try to publish some reports to this. Open Business Intelligence Management Studio and create a Reporting Services project. If you have AdventureWorks 2008 Sample Report Project, you can use it for testing. Once the project is open, get the properties of the project. Set following properties;
Now deploy reports and see. Reports will be published to SharePoint site we created. Once published, open the home page of the site (http://dp03:10005) and click on Reports link in left pane. It lists all the reports published.
Click one of reports and see whether it works fine. Here is the output of Company Sales 2008 report.
All done. Reporting Services 2008 R2 is integrated with SharePoint 2010. I will be exploring more on this, specially SharePoint related things. Once they are explored, I will publish them.
PART III
Configuring Reporting Services 2008 R2 in SharePoint 2010 Integrated mode is straight forward and simple. If you have read my post Configuring Reporting Services 2008 R2 in SharePoint 2010 Integrated Mode – Part I and Configuring Reporting Services 2008 R2 in SharePoint 2010 Integrated Mode – Part , then you know the way of configuring it. You will not face much issues after configuring if it is done with Standalone SharePoint 2010 architecture. If it is a SharePoint farm which components are distributed, few things should be considered. Here are some of points you need to consider when Reporting Services is configured in SharePoint 2010 Farm. Note that this was tested with a Web Application which Authentication is set as Classic and NTLM is set for Authentication Provider.
Domain accounts created
Reporting Services Account: Domain account, no admin privileges. Usually name it as a Domain\ReportingServices. Set this with Reporting Service Configuration Manager.
Application Pool Account: Domain account, no admin privileges. Usually name as Domain\SP_SSRSApp. Use this when the Web Application is created with SharePoint.
Site Collection (or Site) account: Domain account, need local admin rights. Usually name as Domain\SP_Admin.
** All accounts are maintained as Managed Accounts in SharePoint.
Reporting Services Windows Service Account
This should be a domain account. Once it is set with Reporting Services Configuration Manager, you need add the same account to SharePoint too. First, have it as a Managed Account (Central Administration –> Security –> General Security –> Configure Managed Accounts). Then add the account to the Site where you have configured Reporting Services Libraries. You can add this account to Home Members group. Errors you may get if it is not added are “The permissions granted to user ‘Domain\User’ (what ever account you used for logged in) are insufficient for performing this operation.” and “rsAccessDenied”. Note that error has no link with the service account but it is related to it. You may get this error with Report Builder or BIDS when deploying reports, or viewing reports.
In addition to that, you need to make sure that the account is added to Web Application content database which is created for Web Application. If not, you may get and error “Report Server has encountered a SharePoint error.
Other than that, this account is in ReportServer database too. It is automatically added, but make sure that it is there.
Allow Anonymous Setting
Make sure that Allow Anonymous is set as false when the Web Application is created. I faced an issue related deployment from BIDS if this is not set to false (read my
post on it). Once the application is created, double-check with IIS (Site –> Authentication) and see whether it is disabled. As per the Authentication and Authentication Provider we set, Only ASP.NET Authentication and Windows Authentication should be enabled.

Thursday, 14 July 2011

Count the Number of Approved,Rejected and Pending Status in Document Library through CAML Query and Maping it to another List using Event Receiver

 public override void ItemUpdated(SPItemEventProperties properties)
       {
           base.ItemUpdated(properties);
           try
           {
            
        if (properties.List.Title == "SampleLibrary")
               {
                   SPSite oSite = new SPSite("http://sharepoint:4747/");
                   SPWeb oWeb = oSite.OpenWeb();
                   SPList oList = oWeb.Lists["SampleLibrary"];
                   SPQuery spQuery = new SPQuery();
                   spQuery.Query = "<Where><Eq><FieldRef Name='Status' /><Value Type='Text'>Approved</Value></Eq></Where>";
                   SPListItemCollection items = oList.GetItems(spQuery);
                   string s = items.Count.ToString();
                   SPQuery spQuery1 = new SPQuery();
                   spQuery1.Query = "<Where><Eq><FieldRef Name='Status' /><Value Type='Text'>Approval Rejected</Value></Eq></Where>";
                   SPListItemCollection items1 = oList.GetItems(spQuery1);
                   string s1 = items1.Count.ToString();
                   SPQuery spQuery2 = new SPQuery();
                   spQuery2.Query = "<Where><Eq><FieldRef Name='Status' /><Value Type='Text'>Draft In Progress</Value></Eq></Where>";
                   SPListItemCollection items2 = oList.GetItems(spQuery2);
                   string s2 = items2.Count.ToString();
                   SPList oList1 = oWeb.Lists["SampleList"];
                   SPListItem itemToUpdate = oList1.GetItemById(5);
                   itemToUpdate["Status"] = "Approved";
                   itemToUpdate["Count"] = s;
                   itemToUpdate.Update();
                   SPListItem itemToUpdate1 = oList1.GetItemById(6);
                   itemToUpdate1["Status"] = "Approval Rejected";
                   itemToUpdate1["Count"] = s1;
                   itemToUpdate1.Update();
                   SPListItem itemToUpdate2 = oList1.GetItemById(7);
                   itemToUpdate2["Status"] = "Draft In Progress";
                   itemToUpdate2["Count"] = s2;
                   itemToUpdate2.Update();


               }
           }
           catch (Exception exe)
           {
               throw (exe);
           }
          
       }



    }
}

Copy the Items from Top Level Site Document Library to Sub Site Document Libtrary through Event Receiver

 public override void ItemUpdated(SPItemEventProperties properties)
       {
           base.ItemUpdated(properties);
           try
           {
               if (properties.List.Title == "SorceLibrary")
               {
                   {
                       SPSite site1 = new SPSite("http://sharepoint:4747/");
                       SPWeb web1 = site1.OpenWeb();
                       SPListItem myitem = properties.ListItem;
                       if (myitem.File != null)
                       {
                           //string libName = "DestLibrary";
                           SPSite site = new SPSite("http://sharepoint:4747/Products/");
                           SPWeb web = site.OpenWeb();
                           web.AllowUnsafeUpdates = true;
                           // Copy the document to the library
                           SPFolder library = web.Folders["DestLibrary"];
                           library.Files.Add(myitem.Name, myitem.File.OpenBinary());
                           library.Update();
                           web.AllowUnsafeUpdates = false;
                       }

                   }
               }
 }
           catch (Exception exe)
           {
               throw (exe);
           }
          
       }

    }

What is SharePoint?

"SharePoint is a Microsoft's product to develop portals …. And it can be used also to search information in many places (Notes, SAP, …) … and gives you a complete collaboration environment… And includes some advanced functionality like workflows, web forms, excels on web… "