SharePoint Stuff

A collection of helpful tips and resources on maximising the value from a SharePoint Portal Server and Windows SharePoint Services Implementation.

Friday, February 24, 2006

The pointlessness of the - Add(String, Byte[], Microsoft.SharePoint.SPUser, Microsoft.SharePoint.SPUser, System.DateTime, System.DateTime) Method

The Add(String, Byte[], Microsoft.SharePoint.SPUser, Microsoft.SharePoint.SPUser, System.DateTime, System.DateTime) Method in SharePoint allows you to programmatically upload documents and is the only way the "Created By" field can be set to any user other than the one doing the upload.

However according to the MSDN documentation
"This Add method works on a Site Document Library, however, if you use it with an Area Document library, the call will fail with an "Access Denied" error. However, if you do not include the last 4 parameters in the Add method, the file will be added to the Area Document library, but you will not be able to change the Author and document times. "

Gotta love it - this means that the Add method will only allow you to change the Created By for document libraries in the Home Area of the portal (since that is the only Site document library). What is even the point of such a method.

If you try and run it against any other document library you get the following message
"Exception!: Access is denied.(Microsoft.SharePoint)
at Microsoft.SharePoint.h.a(UnauthorizedAccessException A_0)
at Microsoft.SharePoint.Library.a.a(String A_0, String A_1, Object A_2, PutFileOpt A_3, String A_4, String A_5, Int32 A_6, Int32 A_7, Object A_8, Object A_9)"

The only way around is to upload the document using the Add method with no parameters, then directly updating the Created By and Updated By fields in the database using a query such as

update UserData
set tp_author=xx,
tp_editor=yy
where tp_listid='57DF4BF7-F35D-4C6E-AF61-077F6234DDC9'
and tp_id=zz;


Where
xx: is the tp_id from UserInfo table for the user you want to set the Created by Field to. Remember there are multiple users with the same tp_id, so filter down by username as well as site_id
yy: as above but updates the Modified By field.
tp_listid: GUID of the list where the documents exist - find the guid from the docs table
tp_id: is the doclibrowid from the docs table. Remember it is just a sequential number and you need to find the id for the listid you are interested in.

And thats it.

2 Comments:

At 10:41 PM, Anonymous Anonymous said...

Not quite it.

If you're the site admin, you can also specify the author of a new document using that form of the .Add method.

There may be other situations where you can create a doc as another author, and I'm hunting for them now for a project I'm working on. Impersonation doesn't help and I need a user without sharepoint site admin abilities to be able to reassign a document to another user. Ideas?

 
At 7:18 AM, Anonymous Anonymous said...

You're the man!

Thanks for the post!!!

 

Post a Comment

<< Home