Downloading attachment from Sharepoint List: object reference not set to instance of an Object error
Hello,
I am using the following code for downloading attachment from Sharepoint 2007 List.
public static string downloadDoc(string listName, string ListItemID)
{
string filepath = "";
try
{
AEPEx_MOSS.Lists ls = new AEPEx_MOSS.Lists();
//ls.Credentials = System.Net.CredentialCache.DefaultCredentials;
string MOSSWSUser = Microsoft.SSO.Utility.SSOConfigHelper.Read("AEPEx_CarrierInterface", "MOSSWSUserID");
string MOSSWSPWD = Microsoft.SSO.Utility.SSOConfigHelper.Read("AEPEx_CarrierInterface", "MOSSWSUserPWD");
string MOSSWSDomain = Microsoft.SSO.Utility.SSOConfigHelper.Read("AEPEx_CarrierInterface", "MOSSWSUserDomain");ls.PreAuthenticate = false;
System.Net.NetworkCredential cred = default(System.Net.NetworkCredential);
cred = new System.Net.NetworkCredential(MOSSWSUser, MOSSWSPWD, MOSSWSDomain);
ls.Credentials = cred;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
XmlNode ra = default(XmlNode);
ra = ls.GetAttachmentCollection(listName, ListItemID);
string url = ra.InnerText;
SPSite site = new SPSite(Microsoft.SSO.Utility.SSOConfigHelper.Read("AEPEx_CarrierInterface", "LIST_URL"));
SPWeb web = site.OpenWeb();
SPFile list = web.GetFile(url);
string fn = list.Name;
string guid = null;
guid = System.Guid.NewGuid().ToString();
if (!Directory.Exists(Microsoft.SSO.Utility.SSOConfigHelper.Read("AEPEx_CarrierInterface", "MOSSAttachmentPath") + guid))
{
Directory.CreateDirectory(Microsoft.SSO.Utility.SSOConfigHelper.Read("AEPEx_CarrierInterface", "MOSSAttachmentPath") + guid);
byte[] b = list.OpenBinary();
FileStream s = System.IO.File.Create(Microsoft.SSO.Utility.SSOConfigHelper.Read("AEPEx_CarrierInterface", "MOSSAttachmentPath") + guid + "\\" + list.Name);
filepath = Microsoft.SSO.Utility.SSOConfigHelper.Read("AEPEx_CarrierInterface", "MOSSAttachmentPath") + guid + "\\" + list.Name;
s.Write(b, 0, b.Length);
s.Dispose();
s.Close();
}
}
);
}
catch (Exception ex)
{
AEPEx.Integration.BizTalk.CommonUtilities.Logger.WriteError_Filenet("Exception getting Attachment " + ex.Message, EventLogEntryType.Error);}
return filepath;
}I am getting an error:"Object reference not set to Instance of an object" error.
When I run this code from a WIndows app, the ATtachment is getting downloaded properly. However when i run this code by adding reference to a Biztalk APp, I am getting the error,at the line when the new SP Site is getting created. I have searched all over the internet without any solution.- Edited byMike Walsh MVPMVP, ModeratorFriday, July 03, 2009 2:52 PM"i need help desperately, because it is urgent and " removed from Text. These are forums. If you are in a hurry call MS Customer Support Services. If you use forums be prepared to wait and don't push us.
- Edited byRachanaD Friday, July 03, 2009 2:37 PM
Answers
- Hello,
I was able to make the WS work. The way to do it is to share the same Application pool for the web service as of the Sharepoint Shared Service.- Marked As Answer byRachanaD Monday, July 06, 2009 10:04 PM
All Replies
- Does the account that run Biztalk app runs under has rights in SharePoint? The best solution would be to create a custom web service with this functionality and get Biztalk to call it.
My SharePoint Blog - http://www.davehunter.co.uk/blog - How do I go about creating a custom WS to do this?Please suggest.Also how will creating a WS resolve the security issue.
- Hello, I have created a web service for downloading the attachment from Sharepoint.I have set it to run under a user which is an Admin for the MOSS app. However I still get this error: Exception getting Attachment The Web application at http://:1002/_vti_bin/lists.asmx could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application. Please suggest.
- Hello,
I was able to make the WS work. The way to do it is to share the same Application pool for the web service as of the Sharepoint Shared Service.- Marked As Answer byRachanaD Monday, July 06, 2009 10:04 PM

