开发者

Retriving SPListItem through External List's whose Source is from SQL Server 08 Stored Procedure

As mention in the title, I am trying to retrieve data from MSSQL 08 Stored Procedure through external list. As my machine does not have Microsoft Office 2010 installed, I could not use IEntity to filter and retrieve my records; I believe the Filter Classes are kept in the Microsoft.Office.BusinessData.dll. Limiting me to use External List to perform the filtering and retrieving of data.

The external list created by the SharePoint Designer work perfectly; I am able to view and filter the stored procedure by editing the data source filter defined in the view setting.

However my requirement is to programmatically filter and retrieve the items in C#. So my first attempt was simply to query the External List using the SPList.GetItems(SPView) method.

using (SPWeb web = SPContext.Current.Web){
SPList list = web.Lists[contextList.ID];
SPView view = list.Views[contextView.ID];
SPListItemCollection items = list.GetItems(view);
gridview1.DataSource = items.GetDataTable();
gridview1.Databind();
}

However the above approach only manage to get the column name from the list. The data does not seem to be retrieved from the list.

So my second attempt was to run the code in elevated permission.

SPSite contextSite = SPContext.Current.Site;
SPWeb contextWeb = SPContext.Current.Web;
SPList contextList = contextWeb.Lists[listName];
SPView contextView = contextList.Views[0];
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(contextSite.ID))
using (SPWeb web = site.OpenWeb(contextWeb.ID))
{
SPList list = web.Lists[contextList.ID];
SPView view = list.Views[contextView.ID];
SPListItemCollection items = list.GetItems(view);
gridview1.DataSource = items.GetDataTable();<br/>gridview1.Databind();
}
});

Using the 2nd approach, I am faced with "unauthorised permission" exception.

I quite puzzle on why I am facing this issue. Do you guys have any experience in retrieving items from stored procedure through SharePoint BCS? Or are t开发者_如何转开发here better ways to pull data from stored procedures, without having all the codes repeated across different Web Parts?

--------------- Edit 5th Oct -------------------------------

I decide to try execute the stored procedure directly instead of through bcs. But as some of the stored procedure are quite commonly used. Is it possible to share this piece of code so that it can be reuse across different webparts?

-------------- Edit 5th Oct + 4 hours ---------------------

I realize that if i specify a default value for the filter in ECT's read list operation. I would be able to retrieve the item in the list through : SPList.getItems(SPView); The issue now become how to change the filter programmatically. I tried to use SPView.Method, to change the default value. However this does seem to work.

 <Method Name="getTeam">" 
    <Filter Name="code" Value="2"/>"
    </Method> 


Sorry guys, I realize that sharepoint itself provides the API for the filtering classes. is under Microsoft.Sharepoint.Businessdata.runtime;

The filtering classes the usual ComparisonFilter, limit filter etc.

the code to filter the external source look something like

const string entityName = "Name of internal name of the entity";
const string systemName = "name of the external system";
const string nameSpace = "name space of ect";

BdcService bdcservice = SPFarm.Local.Services.GetValue<BdcService>();
IMetadataCatalog catalog = bdcservice.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);
ILobSystemInstance lobSystemInstance = catalog.GetLobSystem(systemName).GetLobSystemInstances()[systemName];
IEntity entity = catalog.GetEntity(nameSpace, entityName);
IFilterCollection filters = entity.GetDefaultFinderFilters();
ComparisonFilter filter= (ComparisonFilter)filters[0];
IEntityInstanceEnumerator enumerator = entity.FindFiltered(filters, lobSystemInstance);
displayTable = entity.Catalog.Helper.CreateDataTable(enumerator);

This piece of code communicate directly with the ECT, thus there is no need to create a external list.

Also it would work for both table or storedprocedure

special thanks to msdn[1] for hinting me to search search through the api again to find the appropriate api [1] http://msdn.microsoft.com/en-us/library/ff798510.aspx


can you try this approach and verify if it works or not (i had a similar problem, i did not use stored procedures however)

using(var web = SPContext.Current.Web)
{
    var list = web.Lists[contextList.ID];
    var query = list.Views[contextView.ID].Query;

    var items = list.GetItems(new SPQuery() { Query = query});

    gridview1.DataSource = items.GetDataTable();
    gridview1.DataBind();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜