Saturday, February 5, 2011

Execute SPDatasource query in Console App?

Is it possible to execute an SPDataSource object query in a console app for testing?

e.g.:

SPDataSource source = new SPDataSource
{
    UseInternalName = true,
    DataSourceMode = SPDataSourceMode.List,
    SelectCommand = "<View/>"
};

source.SelectParameters.Add("WebId", TypeCode.String, "rootweb");
source.SelectParameters.Add("ListName", TypeCode.String, "Contacts");
var c = source.GetView(); 
var d = c.Select();

I think the context info is missing but can't figure out how to add it?

  • I just looked at it in Refelector and it ends up creating a class called SPDataSourceView which depends on SPContext.

    I have never been able to creat an SPContext from a console application because of constructors marked as internal.

    One option would be to put your class into a Web Service that is deployed to your SharePoint Farm. Then have your console application call this Web Service. However you might be better off using one of the Out of Box SharePoint Web Services.

    stuckagain : I was really looking for a way to test CAML queries using an SPDAtaSource without putting it in a Webpart, so making it easier to test.
    JD : Have you looked at the tools out there to build and test CAML queries? http://www.u2u.be/res/Tools/CamlQueryBuilder.aspx
    From JD
  • I´m not sure what you´re after here, I mean

    1. Testing your SPDataSource in console app (nothing to do like said by JD)
    2. Getting data from sharepoint in a datasource manner.

    If your´re going for solution 2 you could use a linqdatasource instead of the spdatasource. See my post on this if that´s what your´re looking for.

    stuckagain : Thanks for that. see my previous comment. In my solution I ended up using a LinqDataSource as you suggested. So much easier :)

0 comments:

Post a Comment