Monday 24 February 2014

Programmatically Adding a Media Web Part

Following code snippet can be used to add a media web part in an existing SharePoint website:
private void AddMediaWebPart()
        {
            const int zoneIndex = 0;
            const string zoneId = "VideoRightZone";
            var webPartGalleryAccessor = new WebPartGalleryAccessor();
            var mediaWebPart = new MediaWebPart
            {
                MediaSource = "/sites/testsite/Documents/Video1.mp4",
                AutoPlay = false,
                Height = 220,
                Width = 370,
                Title = "Bicycle",
 BackImageUrl = "/sites/testsite/PublishingImages/BicycleVideoImage.png",
ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal,
ChromeType =System.Web.UI.WebControls.WebParts.PartChromeType.None
            };
webPartGalleryAccessor.AddMediaWebPartToSitePage(_rootSiteUrl, _subSiteUrl, _pageUrl, mediaWebPart, zoneId, zoneIndex);
           
        }


WebPartGalleryAccessor Class:

 public void AddMediaWebPartToSitePage(string rootSiteUrl, string subSiteUrl, string pageUrl, MediaWebPart webPart, string zoneId, int zoneIndex)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (var pageLoadSite = new SPSite(rootSiteUrl))
                    {
                        pageLoadSite.AllowUnsafeUpdates = true;

                        //using (var pageLoadWeb = pageLoadSite.OpenWeb(subSiteUrl))
                        using (var pageLoadWeb = pageLoadSite.OpenWeb())
                        {
                            pageLoadWeb.AllowUnsafeUpdates = true;
                            AddMediaWebPartToPage(pageLoadWeb, pageUrl, webPart, zoneId, zoneIndex);
                            pageLoadWeb.AllowUnsafeUpdates = false;
                        }
                        pageLoadSite.AllowUnsafeUpdates = false;
                    }
                });

            }
            catch (Exception exception)
            {
                //throw new Exception("Unable to Add WebPart To Site Page: " + pageUrl + " Webpart title: " + webPart.Title + "Error: " + exception.Message);
            }
        }

 private string AddMediaWebPartToPage(SPWeb web, string pageUrl, MediaWebPart webPart, string zoneId, int zoneIndex)
        {
            try
            {
                SPFile file = web.GetFile(pageUrl);
                if (file.CheckOutType == SPFile.SPCheckOutType.None)
                {
                    file.CheckOut();
                }

     using (SPLimitedWebPartManager manager = file.GetLimitedWebPartManager((PersonalizationScope)Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared))
                {

                    manager.AddWebPart(webPart, zoneId, zoneIndex);
                    file.CheckIn("Webpart Added.");
                    return webPart.ID;
                }
            }
            catch (Exception exception)
            {
                throw new Exception("Unable to Add WebPart To Page: " + pageUrl + " Webpart title: " + webPart.Title + "Error: " + exception.Message);
            }
        }

Removing a web part

Following code snippet can be used to remove a web part from an existing SharePoint website:
  1.  using (SPSite spSiteTest = new SPSite(“SiteURL”) 
  2.  {
  3.    using (SPWeb spWebTest = spSiteTest.OpenWeb())
  4.    {
  5.      SPWebPartCollection webparts = spWebTest.GetWebPartCollection("WebPageURL", Storage.Shared);
  6.      for (int k = 0; k < webparts.Count; k++)
  7.      {
  8.        //get reference to webpart
  9.        Microsoft.SharePoint.WebPartPages.WebPart wp = webparts[k];

  10.        //check webpart Title to find webpart which is to be removed
  11.        if (wp.Title == "WebPartName")
  12.        {
  13.          //delete webpart
  14.          webparts.Delete(wp.StorageKey);

  15.          //update spWeb object
  16.          spWebTest.Update();  
  17.       }                                                                                                                                                            }
  18.    }
  19.  } 

Programmatically Adding a Content Editor Web Part

Following code snippet can be used to add a web part in an existing SharePoint website:


  private void AddWebPart()
        {
            const int zoneIndex = 0;
            var webPartGalleryAccessor = new WebPartGalleryAccessor();
            var contentEditor = new ContentEditorWebPart
            {
                ZoneID = "ImageLeftZone",
                Title = "Sample",
                Width = "500px",
                Height = "350px",
                ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal,
                ChromeType =
                    System.Web.UI.WebControls.WebParts.PartChromeType.None
            };
            //Add content 
            XmlDocument xmlDoc = new XmlDocument();
            XmlElement xmlElement = xmlDoc.CreateElement("Root");
            xmlElement.InnerText =            
                   "<img src='/sites/testsite/_catalogs/masterpage/image/video-boximg.JPG' width='370' height='220' />";
            contentEditor.Content = xmlElement;
            contentEditor.Content.InnerText = xmlElement.InnerText;
            contentEditor.Title = "Products";
            webPartGalleryAccessor.AddWebPartToSitePage(_rootSiteUrl, _subSiteUrl, _pageUrl, contentEditor, zoneIndex);
        }

WebPartGalleryAccessor Class:

 public void AddWebPartToSitePage(string rootSiteUrl, string subSiteUrl, string pageUrl, WebPart webPart, int zoneIndex)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (var pageLoadSite = new SPSite(rootSiteUrl))
                    {
                        pageLoadSite.AllowUnsafeUpdates = true;                      
                        using (var pageLoadWeb = pageLoadSite.OpenWeb())
                        {
                            pageLoadWeb.AllowUnsafeUpdates = true;
                            AddWebPartToPage(pageLoadWeb, pageUrl, webPart, zoneIndex);
                            pageLoadWeb.AllowUnsafeUpdates = false;
                        }
                        pageLoadSite.AllowUnsafeUpdates = false;
                    }
                });

            }
            catch (Exception exception)
            {
                //throw new Exception("Unable to Add WebPart To Site Page: " + pageUrl + " Webpart title: " + webPart.Title + "Error: " + exception.Message);
            }
        }


private string AddWebPartToPage(SPWeb web, string pageUrl, WebPart webPart, int zoneIndex)
        {
            try
            {
                SPFile file = web.GetFile(pageUrl);
                if (file.CheckOutType == SPFile.SPCheckOutType.None)
                {
                    file.CheckOut();
                }

                using (SPLimitedWebPartManager manager = file.GetLimitedWebPartManager((PersonalizationScope)Microsoft.SharePoint.Client.WebParts.PersonalizationScope.Shared))
                {
                    manager.AddWebPart(webPart, webPart.ZoneID, zoneIndex);
                    file.CheckIn("Webpart Added.");
                    file.Publish("");
                    file.Approve("");
                    return webPart.ID;
                }
            }
            catch (Exception exception)
            {
                throw new Exception("Unable to Add WebPart To Page: " + pageUrl + " Webpart title: " + webPart.Title + "Error: " + exception.Message);
            }
        }

Configure Business Data Connectivity Service SharePoint 2013

SharePoint’s Business Connectivity Service (BCS) is intended to provide SharePoint access to a variety of data sources to include:
  • SQL Server
  • .NET
  • WCF Service
  • SOAP
  • REST Service Connection
  • XML file connection
Business Connectivity Services works.
  1. BCS Definitions are based on ‘operations’, Create, Read Item, Read List, Update and Delete
  2. BCS allows you to work directly with data stored in an external system or database
  3. Authentication can be done directly using User authentication or the SharePoint Secured Store Service can be used to authenticate with a specific account
  4. Information can be one-way (view only) or two-way (add, update & delete)
  5. BCS data sources can be ‘associated’ to one another to create ‘relationships’ – One-to-Many, One-to-One, etc.
  6. BCS Connections can be made using SharePoint Designer (easiest) or through Visual Studio
  7. BCS ‘definitions’ can be exported and imported across sites
  8. BCS is based on the “Business Data Catalog” and uses BDC definitions to access “line of business” (LOB) data.
  9. WCF provides the most flexibility and control of data and is best when multiple data sources must be combined to create the desired view
Step 1: Configure the BCS on following link steps.

Step 2: Ctreate external list in following link step
http://www.codeproject.com/Articles/683749/Implementing-Business-Data-Connectivity-in-SharePo

Sometime following error occurred in the  BCS:

Error 1:
There are no addresses available for this application.
Solution:
Go to Central Administration --> Application Management --> Under Service ApplicationsClick Manage services on server. Start business data connectivity service.IIS Reset.


Error 2: 
An error has occurred in the providers configured from this site collection
Solution:
Error 3: Database Connector has throttled the response. The response from database contains more than '2000' rows. The maximum number of rows that can be read through Database Connector is '2000'. The limit can be changed via the 'Set-SPBusinessDataCatalogThrottleConfig' cmdlet.

Solution:Execute the following powershell commands:
Set-SPBusinessDataCatalogThrottleConfig -Identity $BCSThrottle -Maximum 1000000 -Default 20000

 $BCSThrottle = Get-SPBusinessDataCatalogThrottleConfig -Scope database -ThrottleType items -ServiceApplicationProxy $bcs

$BCSThrottle

Reference:

PowerView in SharePoint Server

Step 1: Go to the Microsoft SQL Server 2012, click Configuration Tools, and then click the PowerPivot for SharePoint 2013 Configuration

configure the next step based on the following link.
http://rajeshagadi.blogspot.in/2013/07/installing-powerpivot-for-sharepoint.html

Step 2: In Sharepoint, Navigate to site collection and Click Settings.
In the Site Collection Administration group Click Site Collection Features.
Find Power View Integration Feature and PerformancePoint Service Site Collection Feature in the list.
Click Activate. The feature status will change to Active.

Sometime following error occurred  in the Excel service:

Error 1:
The following data connection file does not exist or you do not have permissions to it: 
 The following connections failed to refresh: 
Solution
Change Connection String in Report files.

Error 2:
An error occurred while accessing Secure Store Service. The following connections failed to refresh
Solution:
IIS Reset

Error 3: An error occurred while loading the model for the item or data source 'EntityDataSource'. Verify that the connection information is correct and that you have permissions to access the data source.
Solution:
Restart the following service:
SQL Server (MSSQLSERVER)
SQL Server (POWERPIVOT)
SQL Server Agent (MSSQLSERVER)
SQL Server Agent (POWERPIVOT)
SQL Server Analysis Services (MSSQLSERVER)
SQL Server Analysis Services (POWERPIVOT)
and
IIS Reset


Error 3: We're sorry. We ran into a problem completing your request.
Please try that again in a few minutes.
Solution:


Configure Excel Services in SharePoint 2013

Step 1:
Central Administration > System Settings > Manage services on server
Start "Excel Calculations Services"

Step 2:
Central Administration > Application Management > Manage service applications and create  a new "Excel Service Application"
                            Figure 4.jpg
Figure 5.jpg


  • Click OK You are done with configuring Excel Services.


Step 3:

Central Administration > Application Management > Manage service applications > Excel Service Application (This will change based on how you named the service instance on the previous step) > Trusted File Locations

            
Fill the Address with a Document Library location. I have created a document library called "Excel Documents" in a site called "Team".
           

        
Central Administration > Application Management > Manage service applications > Excel Servcie Application > Trusted Data Connection Libraris
Make sure you have a "Data Connection Library" type library. Name of my data connection library is "Data Connections" which is created in a site called "Team".
Now you have successfully completed with Excel service configuration.

Step 4configure the next step based on the following link.

Sometime following error occurred  in the Excel service:

Error 1:Failed to create the target application because of the following error: Cannot finish this operation successfully. Please contact your administrator.AndAn error has occurred in the claim providers configured from this site collection
Solution:
Configure the Alternate Accessing mapping  in Central administrator,  Intranet box add the http://ServerName:7777


Error 2: Cannot complete this action as the Secure Store Shared Service is not responding. Please contact your administrator.
Solution:

Error 3: Before creating a new Secure Store Target Application, you must first generate a new key for this Secure Store Service Application from the ribbon.
Solution:
OR
IIS Reset

Error 4: We don't know what happened, but something went wrong.
Could you please try that again?
Error Id: 3f645d9c-149d-00e2-edf1-0cbe9f3cc62a, 20131205104839
Solution:
IIS ->Application pool -> start the SharePoint web service root-->IIS Reset.

Error 5: We cannot locate a server to load the workbook Data Model.
Solution:
IIS Reset.

Error 6: We're not able to contact the server. It might not be responding, or your internet connection may have been interrupted. Please try again
Solution:
 Run the firefox.

Error 7: An error occurred while accessing application id ExcelServicesDataAccess from Secure Store Service. The following connections failed to refresh
Solution:
Open SharePoint Central Administration
Open Application Management
Click Manage Service Applications
Click Secure Store Service
Click Set Credentials" button in enter username and password

Error 8:  Be careful. The query to get the data might be unsafe so you should only refresh the workbook if you trust its source.
Do you want to refresh the workbook?
Solution:
In Central Administration, in Application Management, click Manage service applications.
Click Excel Services Application.
Click Trusted File Location.
Click http:// or the location you want to configure.
In External Data, clear the checkbox for Warn on Refresh.
Click OK.

Error 9: An error occurred while working on the Data Model in the workbook. Please try again. 
We were unable to refresh one or more data connections in this workbook. 
The following connections failed to refresh: 
 SalesRetailPowerPivot
Solution:
Start the SharePoint web service root Application pool in IIS and IIS Reset.
http://blogs.technet.com/b/excel_services__powerpivot_for_sharepoint_support_blog/archive/2013/02/01/powerpivot-2013-refreshing-in-the-browser.aspx

Error 10: "You can't open this location using this program. Please try a different location
Solution:  Launch the Server Manager, and click on Add Features
Add Desktop Feature 
Enabling this will require you to restart the server .
 
 

Saturday 22 February 2014

Configure Reporting Services 2012 for SharePoint 2013

Step 1

SharePoint central administration, click Manage services on server in the System Settings group.
Verify the SQL Server Reporting Services Service is installed and in the Running state.

Step 2
Connect to your Central Administration
Go to Manage Service Application
And create the new service “SQL Server Reporting Services Service Application

                        
Step 3:
In site settings, click Site collection Features in the Site Collection Administration group.
The Report Server Integration Feature is active.

Sometime following error occurred in the SQL Server Reporting:
Error 1:
 Web Part Error: A Web Part or Web Form Control on this Page cannot be displayed or imported. The type Microsoft.ReportingServices.SharePoint.UI.WebParts.ReportViewerWebPart, Microsoft.ReportingServices.SharePoint.UI.WebParts, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91 could not be found or it is not registered as safe. Correlation ID: 27a1579c-5445-00e2-0000-0738bef219cc.

SolutionAdd the following line in the web.config file of the sharepoint web application.

<SafeControl Assembly="Microsoft.ReportingServices.SharePoint.UI.WebParts, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.ReportingServices.SharePoint.UI.WebParts" TypeName="*" Safe="True" />

Error 2:
"The Report Viewer Web Control HTTP Handler has not been registered in the application's web.config file.  Add <add verb="*" path="Reserved.ReportViewerWebPart.axd" type = Microsoft.ReportingServices.SharePoint.UI.WebParts.WebPartHttpHandler, Microsoft.ReportingServices.SharePoint.UI.WebParts, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /> to the system.web/httpHandlers section of the web.config file, or add <add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebPart.axd" type="Microsoft.ReportingServices.SharePoint.UI.WebParts.WebPartHttpHandler, Microsoft.ReportingServices.SharePoint.UI.WebParts, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /> to the system.webServer/handlers section for Internet Information Services 7 or later."

SolutionAdd the following line in the web.config file of the sharepoint web application.
<System.web><httpHandlers>
<add verb="*" path="Reserved.ReportViewerWebPart.axd" type = "Microsoft.ReportingServices.SharePoint.UI.WebParts.WebPartHttpHandler, Microsoft.ReportingServices.SharePoint.UI.WebParts, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /></httpHandlers>
And
<System.webServer><handlers>
<add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebPart.axd" type="Microsoft.ReportingServices.SharePoint.UI.WebParts.WebPartHttpHandler, Microsoft.ReportingServices.SharePoint.UI.WebParts, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /></handlers>

Error 3:
The content type with Id 0x010100C3676CDFA2F24E1D949A8BF2B06F6B8B defined in feature {e8389ec7-70fd-4179-a1c4-6fcb4342d7a0} was found in the current site collection or in a subsite. 
Solution: Execute the following power shell command
Enable-SPFeature -Identity E8389EC7-70FD-4179-A1C4-6FCB4342D7A0 -Url http://ssrs.domain.com -force

Error 4 : An error occurred during client rendering.For more information about this error navigate to the report server on the local server machine, or enable remote errors
Solution:
IIS Reset

Note:

If SQL Server Reporting Services is not listed in service page, configure the below link step.

PS C:\Users\Administrator> Install-SPRSService
PS C:\Users\Administrator> Install-SPRSServiceProxy
PS C:\Users\Administrator> get-spserviceinstance -all |where {$_.TypeName -like
"SQL Server Reporting*"} | Start-SPServiceInstance

TypeName                         Status   Id
--------                         ------   --
SQL Server Reporting Services... Provi... 21b26c9b-3be1-406f-929b-cc0afbb1b3ee