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

Discuss Sharepoint Installation, Performance Tuning, Management and other MOSS related aspects here.

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

Post by usenet » Wed Jul 01, 2009 9:42 am

I'm developing my own webpart on MOSS 2007, 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.
More available at Technology Forums
usenet
 
Posts: 80094
Joined: Wed Jun 24, 2009 8:14 am

Re: Programmatically attaching a file to a list item (c#)

Post by usenet » Thu Jul 02, 2009 12:32 pm

I believe that the problem is that the Webparts are Server Side
code. So you either need to do your upload using Javascript on
the client side or you could an the HTML File tag.
http://www.htmlcodetutorial.com/forms/_ ... _FILE.html


On Jul 1, 5:42�am, jman0 wrote:
> I'm developing my own webpart on MOSS 2007, 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.
More available at Technology Forums
usenet
 
Posts: 80094
Joined: Wed Jun 24, 2009 8:14 am


Return to Sharepoint



Who is online

Users browsing this forum: No registered users and 2 guests