2010-11-21 49 views
0

乾草, 我工作的一個Windows窗體應用程序(C#)連接到SharePoint網站,並從中讀取數據(表)使用特定的用戶名/密碼,連接到SharePoint網站通過winform應用程序

問題是: 如何使用特定的用戶名/密碼連接到站點,而不是使用當前的windows用戶或域用戶。

請指點

編輯:P.S:對SP 2007

我的代碼工作:

SPSecurity.RunWithElevatedPrivileges(delegate() 
     { 
      using (SPSite sourceSite = new SPSite(txtBoxSP.Text)) 
      { 
       SPWeb sourceWeb = sourceSite.RootWeb; 
       SPUserCollection usrs = sourceWeb.AllUsers; 

       SPListCollection col = sourceWeb.GetListsOfType(SPBaseType.GenericList); 

       foreach (SPList list in col) 
        System.Diagnostics.Debug.WriteLine(string.Format("My Name: {0}, Type: {1}, Length: {2}", 
         list.Title, list.GetType().ToString(), list.ItemCount.ToString())); 
      } 
     }); 

回答

4

像這樣:

var cc = new CredentialCache(); 
cc.Add(
    new Uri(sourceSite.Url), 
    "NTLM", 
    new NetworkCredential("user", "password")); 
sourceSite.Credentials = cc; 

當然硬中編碼這樣的密碼不是很安全。你可能會問用戶。 NetworkCredential有另一個constructor爲此目的採取一個安全的字符串。


UPDATE:

你WRE使用SharePoint對象模型,我沒有注意到。最初我以爲你是從WSDL生成代理。 Here's an example如何實現這一點。

+0

「Microsoft.SharePoint.SPSite」不包含一個定義爲「憑據」,沒有擴展方法「憑據」接受一個類型的Microsoft.SharePoint程序「第一個參數。 SPSite'可以找到(你是否缺少使用指令或程序集引用?)!請諮詢.. – 2010-11-21 08:52:40

+0

更新:什麼是'網絡'對象?,我猜你提供的關於web部件開發的例子不是winApp,對吧? – 2010-11-21 09:22:31

+0

在這個例子中(編號爲[3]),他們使用一個傳遞給SPSite構造函數的'SPUserToken'。 – 2010-11-21 09:24:36

0

也許這篇文章可以幫助你

MSDN查找此頁給出線。 http://msdn.microsoft.com/en-us/library/ff829215.aspx我覺得這篇文章會告訴你一個方法。告訴我,如果你不能找到這些行:)

Connection Strings 

There may be situations where you need to display data from databases or REST and SOAP services. Accessing data from these data sources requires either connection strings or at least the URL and endpoints of the services to be stored somewhere. Web.config files are an obvious choice but using Web.config files in SharePoint has some caveats. 
The first differentiation we have to make is whether or not we are developing a sandboxed solution. Let’s look at the farm solution first. Farm solutions have access to the file system. Because every site collection is hosted in a web application and every web application has a Web.config file, the Web.config file associated with your farm solution is one place you can store connection strings or other data. The method for doing this is similar to how it is done in ASP.NET development.