The Timer Job works great and saves the files perfectly. Now I needed a Visual Web Part the user could add to their pages which would consume this XML file for the most recent entry, parse the information necessary then show the Stock Price in a pretty fashion per our marketing company's design. Honestly, I did not expect it to be difficult as I have done this a lot outside of .NET and Sharepoint. I setup the code to connect to my list, query the most recent item so I could grab its attachments. All good. With plenty of examples on the Google-nets, I quickly discovered how to get the URL of the attachment and could pass this to an XmlDocument object via Load() which lead to this code:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foreach (SPListItem item in items) | |
{ | |
int thisItemID = item.ID; | |
SPListItem attachItem = list.Items.GetItemById(item.ID); | |
SPAttachmentCollection thisAttach = attachItem.Attachments; | |
// Get URL of first SPListItem Attachment | |
string attachURL = SPUrlUtility.CombineUrl(thisAttach.UrlPrefix, thisAttach[0]); | |
XmlDocument xmlFile = new XmlDocument(); | |
xmlFile.Load(docURL); | |
} |
However, my code would not work and I continually received 404 errors trying to load the document via URL. I could take the same URL I was generating and put it in a browser window and successfully get to the list, but the code did not see it. I looked through logs, used Fiddler and even tried using HttpWebRequest and the LoadXml command as well with nothing shedding light on why this was erroring. I could not find any information from other people having this problem and even posted on MS' forums with no replies (although I was impatient and did not give it long).
My Timer Job was able to access urls outside the network, but this took some tricks by our Network and Server guys due to proxy issues. As such, I assume that maybe there was some permission/proxy issue hiding the list from my code, so I dug for other ways to try and load up this attachment. I found some references to people doing the same type of thing in Sharepoint 2003. Although I hate using old code since you never know what problems may exist using it in newer versions of Sharepoint and .NET, I was racing against my vacation and did not want to leave this hanging for weeks before I got back to it. So I replaced my code with the following which appears to be accessing the file in the list by its local folder structure and passing the binary stream to the XmlDocument Load command.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foreach (SPListItem item in items) | |
{ | |
int thisItemID = item.ID; | |
SPListItem attachItem = list.Items.GetItemById(item.ID); | |
SPAttachmentCollection thisAttach = attachItem.Attachments; | |
// Get the Folder location of the List's attachments | |
SPFolder localAttach = attachItem.ParentList.RootFolder.SubFolders["Attachments"].SubFolders[attachItem.ID.ToString()]; | |
XmlDocument xmlFile = new XmlDocument(); | |
// Send Binary Stream into XmlDocument Load command | |
xmlFile.Load(localAttach.Files[thisAttach[0]].OpenBinaryStream()); | |
} |
So far this is working and I have been able to use XPathNavigator to get the nodes I needed from the XML file. As is my MO, I posted this hoping to help anyone running into this issue and to get feedback from anyone that might have a better understanding of what is going on or a better way to accomplish this.
No comments:
Post a Comment