Programmatically attach a file to a list item (c#)

24 views
Skip to first unread message

jman0

unread,
Jul 1, 2009, 6:07:51 AM7/1/09
to SharePoint Designer 2007 Discussions
I'm developing a custom webpart to substitute the standard webpart on
the NewForm.aspx and EditForm.aspx and create and edit elements on a
list. The list must allow attaching files to list items, so I've
programmed a way to do this, but I have a problem.

If I open the site on the MOSS Server, everything runs fine and the
files get attached properly, but if I do it from any of the clients on
the network, I get "file not found" exceptions. I've tried
impersonating local accounts on the client machines and even service
accounts (i.e. "NT AUTHORITY\NETWORK SERVICE", but I always get a
"incorrect user name or password" exception.

The code which throws the exception is this:

FileStream file = null;

// Read the file. This appears to be the offending line
file = File.OpenRead(Path);

// Path is a string parameter containing the local path to
the file (i.e. c:\folder\file1.txt)

// Store file contents in a byte array
byte[] Content = new byte[file.Length];
file.Read(Content, 0, (int)file.Length);
file.Close();
file.Dispose();

SPAttachmentCollection AttachmentList = Item.Attachments;

// Item is the SPListItem object I'm working with

string FileName = GetFileName(Path);

// GetFileName is a static method I made, to extract the
name of the file from its path

AttachmentList.Add(FileName, Content);

How does MOSS internally accomplish such a thing? I've never had to it
set up so it impersonates local or network accounts, and MOSS always
browses the local drives and attaches the files without trouble.

Thanks in advance.

praveen

unread,
Jul 1, 2009, 7:51:49 AM7/1/09
to SharePoint Designer 2007 Discussions
I know this is the problem i faced few days ago. That's why i placed
the solution on my blog. It never works with the code your written.
Please check the post for solution. -
http://praveenbattula.blogspot.com/2009/06/aspnet-fileupload-on-sharepoint-custom.html

Second chance is,
user don't have access problem. Pass correct credentials or use
RunWithElevatedPrevilizes() method to add attachment with service
account.
Please let me know if you need any help.

http://praveenbattula.blogspot.com

Karthik Reddy

unread,
Jul 1, 2009, 11:38:15 AM7/1/09
to jman0, SharePoint Designer 2007 Discussions
Hi ,
 
Once check whether your clients have sufficient permissions to upload content in the site .If they have read only permission ..they will get the same error as u mentioned .

 
Reply all
Reply to author
Forward
0 new messages