Connecting SharePoint Online and CRM Online using BCS - 2013 Edition-SP online连接CRM online

Connecting SharePoint Online and CRM Online using BCS - 2013 Edition

使用 BCS 连接Sharepoint 2013 online 和CRM online   。 SPO(Sharepoint online)CRM online(Dynamics CRM online)

英文版原址

http://blogs.msdn.com/b/girishr/archive/2013/05/13/connecting-sharepoint-online-and-crm-online-using-bcs-2013-edition.aspx

This blog post is a simple step-by-step walkthrough on connecting SharePoint Online (SPO) to CRM Online using the Business Connectivity Services (BCS). This can be useful in a number of situations including self service portals, executive dashboards, etc. The end goal for this post is to have CRM account information displayed within SharePoint as illustrated below (nothing fancy) but you can easily extend this sample to include additional scenarios such as pulling additional entities/tables, create/update CRM data, charts/dashboard, mashups with ERP and much more.

这篇位置是简单的介绍 如何使用BCS 连接 Sharepoint online和CRM online的 最终目标是如何把CRM的数据显示到Sharepoint online上。这仅仅是一个普通的例子,你可以按照这个例子扩展。例如 图表,仪表板等,当然也可以使用插件和ERP连接。BTW 这我写的,online读取ERP 以目前平台来说 你放弃吧。

Note: SharePoint Online in this example uses SharePoint 2013 and CRM Online is the December 2012 release. Steps are subject to change for future releases. This walkthrough uses theApp model for SharePoint instead of the classical BCS settings managed in the admin center.

注意:这个例子使用的 SP online 连接 CRM online 使用的是APP 模式 而不是原来的BCS模式。 这个程序员都懂哦。

Architecture of the solution    解决方案设计架构。

Below is a quick diagram showing the architecture of the solution we’re going to walkthrough. As you can see from the diagram below, we do have a shim sitting between CRMOnline & SPO. The reason for us to have a shim/proxy web service is because, external authentication is not enabled on CRM Online’s REST endpoint today.  CRMOnline’s REST endpoint today is only available for use by JScript and web resources within the CRM application however you can expect that limitation to change in the future.

下图是一个架构图,就想你从下图了解到,我们用一个SPO连接2个online,其实就是webservice 代理 呵呵 说的CRM的rest 其实我想骂人 CRM的 rest 好像不支持外部

使用,只能期待以后会改善吧。 原话就是 CRM的REST 没有开放外部的连接。所以只能借助WEBservice

Enough with the talk, let’s get to the code.

废话不说,上代码

Building the Proxy OData Service    搭建一个OData服务

    1. Download and extract the latest CRM 2011 SDK if you don’t have it already. If you're using CRM 2013, please ensure that you're following this article withCRM 2013 SDK. While you could build the proxy web service in several ways, we’re going to use an approach that creates a REST/OData service that involves very minimal custom code!
    2. Once you have extracted the SDK, goto the folder “\sdk\walkthroughs\portal\webappwalkthrough\” and open the webappwalkthrough.csproj project using Visual Studio (2012 in this case)
    3. Open the web.config file and change the connection string to your CRMOnline credentials as shown below. In this post, we’re using the same identity for all CRMOnline calls and we are not impersonating the SharePoint user.
    4. 去这个地方下载CRM2011SDK,但是 你不要怕啊 这里名字叫2011 其实可以使用在CRM2013,下载后去那个目录下用VS2012打开,找webconfig文件

    5. Open the CrmData.svc.cs file and modify it as shown in the snippet below. Essentially we’re modifying the code to make the OData endpoint just pull accounts and contacts from CRM instead of pulling everything.

上面这些 不用翻译了吧。 呵呵

                

  
  
  
  
public class CrmData : DataService < Xrm . XrmServiceContext >
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService ( DataServiceConfiguration config )
{
//config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config . SetEntitySetAccessRule ( "AccountSet" , EntitySetRights . AllRead );
config . SetEntitySetAccessRule ( "ContactSet" , EntitySetRights . AllRead );
config . SetServiceOperationAccessRule ( "*" , ServiceOperationRights . All );
config . DataServiceBehavior . MaxProtocolVersion = DataServiceProtocolVersion . V2 ;
}
}

 

view raw CrmData.svc.cs hosted with ❤ by GitHub
 
把下面几个引用加上,注意版本哦
    1. Right-click on the project and “Add Reference” to the Microsoft.IdentityModel.dll (v3.5). Make sure you set it’s copy local property to true.
       
    2. Set CrmData.svc as the start page and hit F5 to verify that the OData endpoint is working locally. You can browse to http://localhost:<devport#>/CrmData.svc/ContactSet to verify that the proxy OData is indeed pulling data from CRM Online.
      Note: For the purposes of quick illustration, the code in this blog post doesn’t apply any security on the proxy OData service. Please refer to Marc Schweigert’sblog post and Steve Fox’s BCS security post for details on implementing security for proxy services.
    3. The service now needs to be deployed publically in order for SharePoint Online to consume it. Here, we’re going to deploy the OData service to the *free*Windows Azure websites. Once you have signed up for Windows Azure, login to your Azure account and hit the “New” button followed by the steps illustrated below to quick create a website.Hint: Use the region closest to your CRMOnline datacenter. The unofficial way of finding this out is documentedhere.
      Connecting SharePoint Online and CRM Online using BCS - 2013 Edition-SP online连接CRM online_第1张图片

 

  1. Once the site has been created, download its publishing profile from the portal.
  2. Back to Visual Studio, right-click on the project and click “Publish”. In the publish dialog, choose Import –> “Import from a publish profile file” –> browse to the publishing profile you downloaded earlier –> OK. You can run through the next few steps or hit publish button directly.
  3. Once the site has been published, browse to the public URL for the service (say http://yoururl.azurewebsites.net/CrmData.svc/AccountSet) and ensure that the endpoint is able to pull data from CRM Online.
    Connecting SharePoint Online and CRM Online using BCS - 2013 Edition-SP online连接CRM online_第2张图片

Creating the SharePoint Online App

Now that we have the proxy OData service up and running, it is time to create the SharePoint App.

    1. In your Office365 admin center, click the Admin dropdown followed by SharePoint. Create a new private site collection with the “Developer Site” template.
      Connecting SharePoint Online and CRM Online using BCS - 2013 Edition-SP online连接CRM online_第3张图片          
    2. Open Visual Studio as admin or better yet follow this hack to always run Visual Studio 2012 as admin. If you’re going to spend a lot of time building SharePoint apps, the hack will save you a lot of time and pain.
    3. Create a new “App for SharePoint 2013” project.
    4. Enter a name for the app, the URL of the site collection you created earlier and choose “SharePoint-hosted” followed by clicking the Finish button.
      Connecting SharePoint Online and CRM Online using BCS - 2013 Edition-SP online连接CRM online_第4张图片
    5. If Visual Studio prompts you for Office365 credentials, enter them so that the project is connected to your SPO site.
    6. Right-click on the project –> Add –> “Content Types for an External Data Source”.
      image
    7. Enter the URL for the custom OData proxy running in Azure website and specify a name. Click Next.
      image
    8. Choose both AccountSet & ContactSet and hit the Finish button. Visual Studio automatically generates the ECTs (External Content Types) for both accounts and contacts.
      Connecting SharePoint Online and CRM Online using BCS - 2013 Edition-SP online连接CRM online_第5张图片
    9. Within Default.aspx (under Pages), change the code within the head and body sections as listed in the below snippet. Essentially we’re creating the links for account and contact ECTs along with a placeholder for some custom javascript. Make sure that the version of jQuery referenced in the code matches the one within the Scripts folder of your app.
          
          
          
          
<%-- The markup and script in the following Content element will be placed in the <head> of the page --%>
<asp:Content ContentPlaceHolderID= "PlaceHolderAdditionalPageHead" runat= "server" >
<script type= "text/javascript" src= "../Scripts/jquery-1.8.2.min.js" ></script>
<script type= "text/javascript" src= "/_layouts/15/sp.runtime.debug.js" ></script>
<script type= "text/javascript" src= "/_layouts/15/sp.debug.js" ></script>
 
<!-- Add your CSS styles to the following file -->
<link rel= "Stylesheet" type= "text/css" href= "../Content/App.css" />
 
<!-- Add your JavaScript to the following file -->
<script type= "text/javascript" src= "../Scripts/App.js" ></script>
 
<!-- The following script runs when the DOM is ready. The inline code uses a SharePoint feature to ensure -->
<!-- The SharePoint script file sp.js is loaded and will then execute the sharePointReady() function in App.js -->
<script type= "text/javascript" >
$ ( document ). ready ( function () {
SP . SOD . executeFunc ( 'sp.js' , 'SP.ClientContext' , function () { sharePointReady (); });
});
</script>
</asp:Content>
 
<%-- The markup and script in the following Content element will be placed in the <body> of the page --%>
<asp:Content ContentPlaceHolderID= "PlaceHolderMain" runat= "server" >
 
<div>
<p id= "message" >
<!-- The following content will be replaced with the user name when you run the app - see App.js -->
initializing...
</p>
</div>
<a href= "../lists/AccountSet" >Accounts Full List </a> <br />
<a href= "../lists/ContactSet" >Contacts Full List </a><br />
<h2>Account List </h2>
<div id= "displayDiv" ></div>
 
</asp:Content>
view raw gistfile1.html hosted with ❤ by GitHub
    1. Next up, let’s write some jQuery to select the AccountNumber & Name and display it in a simple HTML table (sorry web designers)! Open the App.js file (under Scripts folder) and replace it with the code below. If you’re familiar with javascript and jQuery, the code should be pretty easy to follow.
          
          
          
          
var context ;
var web ;
var user ;
 
// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
function sharePointReady () {
context = SP . ClientContext . get_current ();
web = context . get_web ();
 
getUserName ();
};
 
// This function prepares, loads, and then executes a SharePoint query to get the current users information
function getUserName () {
user = web . get_currentUser ();
context . load ( user );
context . executeQueryAsync ( onGetUserNameSuccess , onGetUserNameFail );
}
 
// This function is executed if the above OM call is successful
// It replaces the contents of the 'helloString' element with the user name
function onGetUserNameSuccess () {
$ ( '#message' ). text ( 'Hello ' + user . get_title ());
}
 
// This function is executed if the above call fails
function onGetUserNameFail ( sender , args ) {
alert ( 'Failed to get user name. Error:' + args . get_message ());
}
 
 
$ ( document ). ready ( function () {
 
//Namespace
window . AppLevelECT = window . AppLevelECT || {};
 
//Constructor
AppLevelECT . Grid = function ( hostElement , surlWeb ) {
this . hostElement = hostElement ;
if ( surlWeb . length > 0 &&
surlWeb . substring ( surlWeb . length - 1 , surlWeb . length ) != "/" )
surlWeb += "/" ;
this . surlWeb = surlWeb ;
}
 
//Prototype
AppLevelECT . Grid . prototype = {
 
init : function () {
 
$ . ajax ({
url : this . surlWeb +
"_api/lists/getbytitle('AccountSet')/items?" +
"$select=BdcIdentity,AccountNumber,Name" ,
headers : {
"accept" : "application/json;odata=verbose, */*" ,
"X-RequestDigest" : $ ( "#__REQUESTDIGEST" ). val ()
},
success : this . showItems
});
},
 
showItems : function ( data ) {
var items = [];
 
items . push ( "<table border='1'>" );
items . push ( "<tr><td>Account Number</td>" +
"<td>Account Name</td></tr>" );
 
$ . each ( data . d . results , function ( key , val ) {
items . push ( '<tr id="' + val . BdcIdentity + '"><td>' +
val . AccountNumber + '</td><td>' +
val . Name + '</td></tr>' );
});
 
items . push ( "</table>" );
 
$ ( "#displayDiv" ). html ( items . join ( '' ));
}
}
 
ExecuteOrDelayUntilScriptLoaded ( getAccounts , "sp.js" );
});
 
function getAccounts () {
var grid = new AppLevelECT . Grid ( $ ( "#displayDiv" ),
_spPageContextInfo . webServerRelativeUrl );
grid . init ();
}
view raw App.js hosted with ❤ by GitHub
  1. Right-click on the project or solution and click Deploy. Visual Studio automagically installs the app to your SPO developer site.
  2. Open the SPO site and you should see your app under “Apps in Testing”. Click on the app and make sure it works as expected.
     Connecting SharePoint Online and CRM Online using BCS - 2013 Edition-SP online连接CRM online_第6张图片

   

Now that you have the base connectivity between CRMOnline and SPO through BCS, the possibilities are endless. You can bring data from other CRM entities, apply security trimming, have users update CRM data directly from SharePoint, use highcharts.com for pretty charts and dashboards, mashup CRM data with other OData data sources and much more depending upon your needs. If you end up building interesting SharePoint-CRM apps, please do tell us through comments below.

现在你可以通过BCS连接Sharepoint online 和CRM online了 ,这可以做很多事,例如图片,仪表板,ERP数据等等,能完成你所有需要的很多是。下面是有什么问题给我留言。

 

 

 

你可能感兴趣的:(Connecting SharePoint Online and CRM Online using BCS - 2013 Edition-SP online连接CRM online)