Wednesday, October 01, 2008

Read SharePoint documents using SQL Server query

Retrieve documents form the SharePoint website and display the same in your Windows forms application. No need to use SharePoint webservices or SharePoint Object model. Just run through the query to fetch the documents from the SharePoint using SQL query.
Sample Code:
private void btnload_Click(object sender, EventArgs e)
{
string RootSite = txtsharepointsite.Text.ToString(); // "http://w2czbmlg01:12345/";
// Opening SQL connection for Microsoft SQL Server 2005
SqlConnection MySQLConnection = new SqlConnection(@"Data Source=W2CZBMLG01;Initial Catalog=WSS_Content_12345;Integrated Security=True");
// Passing SQL command text
string strSQLcmd = "SELECT CONVERT(nvarchar(36), Id) AS ID, '" + RootSite + "'+ DirName + '/' + LeafName AS Name, LeafName FROM dbo.Docs WHERE (LeafName NOT LIKE 'template%') AND LeafName LIKE '%.doc' ORDER BY DirName, LeafName";
SqlCommand MySQLCommand = new SqlCommand (strSQLcmd, MySQLConnection);
MySQLConnection.Open();
MySQLCommand.ExecuteNonQuery();
// Dats set
DataSet Docds = new DataSet("Docs");
SqlDataAdapter MySQLAdapter = new SqlDataAdapter();
MySQLAdapter.SelectCommand = MySQLCommand;
MySQLAdapter.Fill(Docds, "Docs");
for (int i = 0; i < Docds.Tables["Docs"].Rows.Count; i++)
{
// list populates
lstdocuments.Items.Add(Docds.Tables["Docs"].Rows[i].ItemArray[2].ToString());
//lstdocuments.DisplayMember = Docds.Tables["Docs"].Rows[i].ItemArray[2].ToString();
//lstdocuments.ValueMember = Docds.Tables["Docs"].Rows[i].ItemArray[1].ToString();
}
}
}


Just create a Windows Form application and add this code in the form load or button click to get the result.

1 comment:

Gil Salcedo said...

Direct access to the database as you suggest is not supported by Microsoft.

ASP.NET MVC - Sport Facility Booking system

  The project relies on some external service providers. Thus accessing them via their API requires authentication. An API Key need to be su...