Friday, November 27, 2009

Downloading Attachments from a List

So I was asked to get all attachments from a List. Now, there are several articles on the web that show you how to leverage the Object Model, so I'm not going to get into that. What I was asked to do was to connect to a remote SharePoint instance and bring down all attachments found in a list.

My first inclination was to leverage the Object Model, but after spending an hour on it, I remembered, I can't connect to a remote instance via the Object Model. So I was left with Web Services. The solution wasn't bad at all either. Once you've connected to Lists service, you can call the GetAttachmentCollection method to return attachments per list item. I'm assuming that everyone knows how to grab List ids and ListItem ids via Web services, so I'll skip that tutorial.

Here's the code:

XmlNode attachmentColl = wsListObj.GetAttachmentCollection(listIDStringWithBrackets, listItemIDString);

for (int i = 0; i < attachmentColl.ChildNodes.Count; i++)
{
string[] attachmentArr = attachmentColl.ChildNodes[i].InnerXml.Split(new char[] { '/' });

System.Net.WebRequest file = System.Net.WebRequest.Create(attachmentColl.ChildNodes[i].InnerXml);

CopyFileToFolder(file.GetResponse().GetResponseStream(),
attachmentArr[attachmentArr.Length - 1]);

file = null;
}

Tuesday, November 17, 2009

Reusing the SSP LDAP Connection

So, I was asked to generate a new SPList in MOSS 2007 based on a specific AD group. And, as I brainstormed, I recalled an AD connection setup within SSP to import user profiles. "Now, wouldn't it be nice to leverage the SSP LDAP connection to setup my LDAP connection," I thought. Well, it would be nice indeed and would take away the headache of having to setup a new webconfig item for my LDAP.
And thus, I scoured the web for articles that focused in on this technique for grabbing LDAP connectivity, but my search turned cold pretty quick. Since I'm so hard-headed, I decided I'd uncover this gem myself and here is what I discovered.

By getting an instance of the UserProfileConfigManager, I immediately had access to the available datasources in SSP. With a UserProfiles.Datasource in hand, I enumerated the connections and looped through until I found the one I wanted. With that, I now had the information I needed to build my LDAP connection. Check it out...

------------------------------------------------------------------
string ldapConnectionPath = string.Empty;
UserProfileConfigManager configMgr = new UserProfileConfigManager(ServerContext.Current);
DataSource adDataSource = configMgr.GetDataSource();

System.Collections.IEnumerator ConnEnumerator = adDataSource.Connections.GetEnumerator();
ConnEnumerator.Reset();

while (ConnEnumerator.MoveNext())
{
if(ConnEnumerator.Current.GetType().Name.Equals("LDAPConnection"))
{
LDAPConnection adConn = (LDAPConnection)ConnEnumerator.Current;
ldapConnectionPath = string.format("LDAP://{0}:{1}/{2}",
adConn.Server,
adConn.Port,
adConn.SearchBase);

// adConn.DomainName // Domain Name is also available
break;
}
}