0

I want to upload files in a document library of sharepoint site (WSS 3.0) using c#, but the problem is that I want to allow only authorized users to upload file. I don't know how to pass user credentials using sharepoint object model. Is there a way to get user credentials from user i.e username, password and then upload file using these credentials. I am asking this because if i use elevated prevelages then I don't know who uploaded the file because file uploaded as system account.

Alex
  • 31
  • 1
  • 3

2 Answers2

1

Normally whenever you upload a file to SharePoint library its done using current logged in user context so if any user who has not got sufficient rights will not be able to upload a file and you will get a exception for the same in the code

  • hi Ashutosh,Actually I am uploading file programatically using sharepoint object model. File uploaded successfully using following code but my problem is that I want to get user name and password from user, so that file upload using given user's credentials. – Alex Dec 02 '10 at 13:01
  • using (SPSite oSite = new SPSite(sharePointSite)) { using (SPWeb oWeb = oSite.OpenWeb()) { SPFolder myLibrary = oWeb.Folders[documentLibraryName]; FileStream fileStream = File.OpenRead(fileToUpload); myLibrary.Files.Add(FileName, fileStream, replaceExistingFiles); } } – Alex Dec 02 '10 at 13:02
  • Hi Alex, the code you have written will always run in context of logged in user unless you use spelevated prevelages which uses app pool account credentials. So basically you do not need to pass any extra userid asif the logged in user does not have appropriate privalages he would not be able to upload the files into document library – Ashutosh Singh-MVP SharePoint Dec 02 '10 at 13:06
  • Ashutosh Thanks for ur reply, yes you are right but what about if he want to upload files as a different user. i.e currently he is logged in as administrator but he want to upload files using another account (may be contributor account). – Alex Dec 02 '10 at 13:18
  • I would suggest use webservice to upload the content in that case or ask your admin user to use sign in as a different user functionality available with SharePoint – Ashutosh Singh-MVP SharePoint Dec 02 '10 at 13:29
  • http://stackoverflow.com/questions/31868/upload-a-file-to-sharepoint-through-the-built-in-web-services – Ashutosh Singh-MVP SharePoint Dec 02 '10 at 14:00
0

It needs to be custom code and wouldn't be available out of the box.

One approach. You can pass the username to the routine that does the file uploading. You can then enumerate the site users to see a) if the user exists on the site and b) do they have the proper rights to add the document.

Look at the AllUsers property on the SPWeb object (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.allusers.aspx)

John Ptacek
  • 1,886
  • 1
  • 15
  • 20