使用 C# 在 sharepoint 2007 中访问列表

发布于 2024-10-31 13:30:53 字数 949 浏览 0 评论 0原文

我希望从 sharepoint 2007 中的一些不同的客户列表中编译数据

,它是一个托管的 sharepoint 站点,因此我无法访问计算机后端。

有没有使用 C# 访问 sharepoint 站点的示例代码?

这是到目前为止我的代码(我收到错误无法连接到 Sharepoint 站点''。稍后再试。

    DataSet dt = new DataSet();
    string query = "SELECT * FROM list";
    string site = "http://sp.markonsolutions.com/Lists/Security/";
    string list = "35E70EO4-6072-4T55-B741-4B75D5F3E397"; //security db
    string myConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=2;RetrieveIds=Yes; DATABASE="+site+";LIST={"+list+"};";
    OleDbConnection myConnection = new OleDbConnection();
    myConnection.ConnectionString = myConnectionString;
    OleDbCommand myAccessCommand = new OleDbCommand(query,myConnection);
    OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
    myConnection.Open();



          myDataAdapter.Fill(dt);

    //execute queries, etc
    myConnection.Close();

I'm looking to compile data from a few diffrent custome lists in sharepoint 2007

Its a hosted sharepoint site so I don't have access to the machine backend.

Is there any example code to access the sharepoint site using c#?

here is my code thus far (i get the error Cannot connect to the Sharepoint site ''. Try again later.)

    DataSet dt = new DataSet();
    string query = "SELECT * FROM list";
    string site = "http://sp.markonsolutions.com/Lists/Security/";
    string list = "35E70EO4-6072-4T55-B741-4B75D5F3E397"; //security db
    string myConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=2;RetrieveIds=Yes; DATABASE="+site+";LIST={"+list+"};";
    OleDbConnection myConnection = new OleDbConnection();
    myConnection.ConnectionString = myConnectionString;
    OleDbCommand myAccessCommand = new OleDbCommand(query,myConnection);
    OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
    myConnection.Open();



          myDataAdapter.Fill(dt);

    //execute queries, etc
    myConnection.Close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神魇的王 2024-11-07 13:30:53

如果您无法在 SharePoint 计算机上部署代码,那么您几乎必须使用 Web 服务。

列表网络服务就是您所追求的。

它位于 http://yousharepointsite.com/_vti_bin/Lists.asmx 上,并且默认情况下应处于打开状态。请注意,如果您的站点配置了FBA,则在查询lists.asmx之前,您必须使用_vti_bin/Authentication.asmx登录。

这是一篇文章,提供了您需要的所有信息:

http://sharepointmagazine.net/articles/writing-caml-queries-for-retriving-list-items-from-a-sharepoint-list

由于上述原因,请跳过使用对象模型来查询 SharePoint 列表并直接转至使用 SharePoint Web 服务通过 CAML 检索列表项。

这篇文章非常完整,所以我认为你应该对此感到满意。

根据您的编辑,我认为您无法像这样创建到远程站点的连接。您无法像那样使用 SQL 查询 SharePoint,您确实需要使用 CAML...

添加对 Web 服务的引用后:

ListService listsClient = new ListService.Lists();
listsClient.Url = @"http://sp.markonsolutions.com/" + @"/_vti_bin/lists.asmx";
listsClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
listsClient.GetListItems(...);

阅读有关 GetListItems 此处

正如我所说,您需要使用 Web 服务。如果您尝试创建像这样的连接来直接查询数据库,那么您将走向死胡同。不推荐。

If you can't deploy code on the SharePoint machine, then you pretty much have to use the web services.

The lists web service is what you're after.

It will be located on http://yousharepointsite.com/_vti_bin/Lists.asmx and should be open by default. Note that if your site is configured with FBA, you will have to use the _vti_bin/Authentication.asmx to log in before you query lists.asmx.

Here is an article that gives all the information you need :

http://sharepointmagazine.net/articles/writing-caml-queries-for-retrieving-list-items-from-a-sharepoint-list

For the reasons mentioned above, skip the part on using the object model to query SharePoint lists and go directly to Retrieving list items with CAML using the SharePoint web services.

The article is pretty complete, so I think you should be OK with that.

As per your edit, I don't think that you can create a connection to your remote site like that. You can't query SharePoint with SQL like that, you really need to use CAML...

Once you've added the reference to the web service :

ListService listsClient = new ListService.Lists();
listsClient.Url = @"http://sp.markonsolutions.com/" + @"/_vti_bin/lists.asmx";
listsClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
listsClient.GetListItems(...);

Read more on the GetListItems here

Like I said, you need to use the web services. You are heading towards a dead end if you are trying to create connection like that to query the database directly. It is not recommended.

诠释孤独 2024-11-07 13:30:53

不确定您尝试执行的操作是否可行,除非您使用适用于 SharePoint 的 ado.net 连接器,请查看 http: //www.bendsoft.com/net-sharepoint-connector/

它使您能够与 SharePoint 列表进行对话,就像它们在普通 sql 表中一样

在示例中插入一些数据

public void SharePointConnectionExample1()
{
    using (SharePointConnection connection = new SharePointConnection(@"
                Server=mysharepointserver.com;
                Database=mysite/subsite
                User=spuser;
                Password=******;
                Authentication=Ntlm;
                TimeOut=10;
                StrictMode=True;
                RecursiveMode=RecursiveAll;
                DefaultLimit=1000;
                CacheTimeout=5"))
    {
        connection.Open();
        using (SharePointCommand command = new SharePointCommand("UPDATE `mytable` SET `mycolumn` = 'hello world'", connection))
        {
            command.ExecuteNonQuery();
        }
    }
}

或选择列表数据到 DataTable

string query = "SELECT * FROM list";
conn = new SharePointConnection(connectionString);
SharePointDataAdapter adapter = new SharePointDataAdapter(query, conn);

DataTable dt = new DataTable();
adapter.Fill(dt);

或使用填充 DataGrid 的辅助方法

string query = "Select * from mylist.viewname";
DataGrid dataGrid = new DataGrid();
dataGrid.DataSource = Camelot.SharePointConnector.Data.Helper.ExecuteDataTable(query, connectionString);
dataGrid.DataBind();
Controls.Add(dataGrid);

Not sure if what you try to do is possible unless you use an ado.net connector for SharePoint, have a look at http://www.bendsoft.com/net-sharepoint-connector/

It enables you to talk to SharePoint lists as if they where ordinary sql-tables

In example to insert some data

public void SharePointConnectionExample1()
{
    using (SharePointConnection connection = new SharePointConnection(@"
                Server=mysharepointserver.com;
                Database=mysite/subsite
                User=spuser;
                Password=******;
                Authentication=Ntlm;
                TimeOut=10;
                StrictMode=True;
                RecursiveMode=RecursiveAll;
                DefaultLimit=1000;
                CacheTimeout=5"))
    {
        connection.Open();
        using (SharePointCommand command = new SharePointCommand("UPDATE `mytable` SET `mycolumn` = 'hello world'", connection))
        {
            command.ExecuteNonQuery();
        }
    }
}

Or to select list data to a DataTable

string query = "SELECT * FROM list";
conn = new SharePointConnection(connectionString);
SharePointDataAdapter adapter = new SharePointDataAdapter(query, conn);

DataTable dt = new DataTable();
adapter.Fill(dt);

Or using a helper method to fill a DataGrid

string query = "Select * from mylist.viewname";
DataGrid dataGrid = new DataGrid();
dataGrid.DataSource = Camelot.SharePointConnector.Data.Helper.ExecuteDataTable(query, connectionString);
dataGrid.DataBind();
Controls.Add(dataGrid);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文