Archive for March, 2009
Sharepoint/WSS – Getting a list of files in a folder
Recently I had a request from a client to upload a random set of files on a user’s desktop folder into a SharePoint folder. This part of it was not tricky since you can use the System.Net.WebClient.UploadFile() method to accomplish the task. The problem I encountered was that the UploadFile() method does not check for an existing file with the same name on the specified SharePoint folder.
For this reason, I created a custom function that returns to me the list of files in a SharePoint Folder. I could then use this to warn the user if a file with the same name already exists on the SharePoint folder and have him/her take a manual action. The function uses the SharePoint List Web Service GetListItems() method and parses the XML in order to get the files in the folder queried.
Enjoy!
Private Function GetListOfFilesInSPFolder(ByVal listsWS As WebSvcLists.Lists, _ ByVal folderPath As String) As List(Of String) Dim _files As List(Of String) = New List(Of String) ' set up xml doc for getting list of files under a folder Dim doc As XmlDocument = New XmlDocument() Dim queryOptions As XmlElement = doc.CreateElement("QueryOptions") queryOptions.InnerXml = "<Folder>" & folderPath & "</Folder>" ' get the list of files Dim listItemsNode As XmlNode = listsWS.GetListItems( _ WssDocumentLibraryRootFolder, Nothing, _ Nothing, Nothing, Nothing, queryOptions, Nothing) Dim xmlResultsDoc As XmlDocument = New XmlDocument() xmlResultsDoc.LoadXml(listItemsNode.OuterXml) Dim ns As XmlNamespaceManager = New XmlNamespaceManager(xmlResultsDoc.NameTable) ns.AddNamespace("z", "#RowsetSchema") For Each row As XmlNode In xmlResultsDoc.SelectNodes("//z:row", ns) _files.Add(row.Attributes("ows_LinkFilename").Value) Next Return _files End Function
Automate Certificate Install for XBAP Application
An XBAP application is a WPF application that is targeted to work under the security sandbox within a browser. Because of these restrictions, there is very little you can do to interact with the local computer running the code or even access other web services outside of your main server. XBAP gets around this by allowing you to mark your application as full or partial trust. The following post gives a pretty good overview of how to set up a project to do this:
Setup XBAP Project Full Trust
Although this all works, the part of setting up the user’s browser to accept the trust needed by your application is a bit complicated for the average user to follow. For this reason, I wanted to provide a bit of source code that you can use to create an automated program to install the required certificate on the user’s computer.
All that you have to do is place the pfx file you created via Visual Studio on a server that your users will have access to. The user will only need to download a zip file that contains an executable and run it. This executable will automate the work of installing your certificate on their computer which should make your application richer with full trust and make the barrier of entry for a novice user to start using your XBAP application a lot lower.
Enjoy!
using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Forms;
namespace CertInstall
{
class Program
{
static void Main(string[] args)
{
try
{
//we will use the web client to download the pfx file
//from the server for installation on the local machine
WebClient webClient = new WebClient();
// if you need to pass credentials, this is how you do that
//webClient.Credentials =
new System.Net.NetworkCredential("username", "password");
byte[] certBytes =
webClient.DownloadData("http://HostName/TheCertKey.pfx");
if (certBytes.Length > 0)
{
Console.WriteLine("Begin ....");
createCert(StoreName.TrustedPublisher, certBytes);
createCert(StoreName.Root, certBytes);
createCert(StoreName.My, certBytes);
Console.WriteLine("Finished ....");
}
else
{
Console.WriteLine("error.");
Console.ReadLine();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + Environment.NewLine +
ex.StackTrace);
}
}
private static void createCert(StoreName storageName,
byte[] certBytes)
{
// this is the password you used for the pfx file
//when you created it
X509Certificate2 cert =
new X509Certificate2(certBytes, "pfxpassword");
X509Store store =
new X509Store(storageName, StoreLocation.CurrentUser);
store.Open(OpenFlags.MaxAllowed);
store.Add(cert);
store.Close();
}
}
}