'Connect to Sharepoint site using specific username/Password through WinForm App
Hay, I'm working on a windows form application (C#) that connects to a sharepoint site and read data from it (lists),
the question is: how can I connect to the site using specific username/Password, and not using the current windowsUser or domain user.
Please advice
Edit: P.S.: working on SP 2007
My code:
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()));
}
});
Solution 1:[1]
Like this:
var cc = new CredentialCache();
cc.Add(
new Uri(sourceSite.Url),
"NTLM",
new NetworkCredential("user", "password"));
sourceSite.Credentials = cc;
Of course hard-coding a password like this is not very secure. You might ask the user about it. NetworkCredential has another constructor taking a secure string for this purpose.
UPDATE:
I didn't notice you wre using the SharePoint object model. Initially I thought you were generating a proxy from the WSDL. Here's an example of how to achieve this.
Solution 2:[2]
Maybe this article can help you
From MSDN Find given lines in this page. http://msdn.microsoft.com/en-us/library/ff829215.aspx I think this article will show you a way. Tell me if you cant find these lines :)
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Glorfindel |
| Solution 2 | Serkan Hekimoglu |
