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()));
}
});
Like this:
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.