Tuesday, February 26, 2013

Programmatically assign a SharePoint user to a "Person or Group" column in a SharePoint list

Here is some sample code that demonstrates how you can assign a given SharePoint user to a "Person or Group" column or field (in this case, I have a custom "Person or Group" field called "Owner") contained within a SharePoint list:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

...

Guid siteId = new Guid("GuidOfSiteCollection");
Guid webId = new Guid("GuidOfSite");
Guid listId = new Guid("GuidOfList");
string itemTitle = "My Item's Title";
string userName = "Domain//UserName";

using (SPSite site = new SPSite(siteId))
{
    using (SPWeb web = site.OpenWeb(webId))
    {
        // Find all list items that match your search criteria
        SPQuery query = new SPQuery();
        query.Query = string.Format("{0}", itemTitle);
        SPListItemCollection items = web.Lists[listId].GetItems(query);
        // Obtain a reference to SPUser object associated with the specified user
        SPUser user = web.AllUsers[userName];
        // Assign the user to the custom "Owner" field of my SharePoint list items
        web.AllowUnsafeUpdates = true;
        foreach (SPListItem item in items)
        {
            if (user != null)
            {
                item["Owner"] = user;
                item.Update();
            } 
        }
        web.AllowUnsafeUpdates = false;
    }
}
As you can tell, this is a super-simplified version of some actual code that I've got working. Naturally, you will want to organize this code to better suit your needs (as well as add some exception handling), but the purpose of this example is to show you the basics of how it's done. Enjoy!

Monday, February 25, 2013

How to obtain a copy of the InfoPath template file associated with a SharePoint Form Library

Here are the steps you can use for obtaining a copy of the InfoPath template file (i.e. template.xsn) after it has been published to a SharePoint form library:

  1. Open IE and navigate to the SharePoint site where your forms library exists
  2. Click on Site Actions -> View All Site Content
  3. Under the Document Libraries section, locate and click on the link for your form library
  4. On the Library Tools tab, click on Library


  5. Locate the Connect & Export section and click on Open with Explorer


  6. When Windows Explorer pops up, click on the Forms directory


  7. Locate the "template.xsn" file


  8. Copy and paste the "template.xsn" file to a location on your local hard drive
You may now edit that local copy of the template.xsn file using InfoPath and publish the form back to the SharePoint form library (i.e. the path information was saved to the form when it was published).

Tuesday, February 19, 2013

Programmatically obtain a list of files contained in a directory

If you ever need to programmatically obtain information about files contained within a given directory/folder, I've provided some sample code from a Windows Form Application I created that will provide you with that capability.  In this particular instance, my Windows App allows the user to select the directory he/she wishes to review and then populates a Grid View control with information about the files contained in the directory (i.e. in this simple example, the name of the file and the date the file was last modified):

using System.Windows.Forms;
using System.Data;
using System.IO;

public partial class Form1 : Form
{
        private FolderBrowserDialog m_fbd;

        ...

        /// <summary>
        /// When the button is clicked, the user will be prompted to select the input file to open via the OpenFileDialog
        /// control.  Once the user has selected the input file, the information in the file will be read, parsed, and then
        /// displayed in the grid view control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReviewFiles_Click(object sender, EventArgs e)
        {
            if (m_fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("FileName");
                dt.Columns.Add("LastModifiedDate");

                DirectoryInfo dir = new DirectoryInfo(m_fbd.SelectedPath);
                foreach (FileInfo file in dir.GetFiles("*.*"))
                {
                    DataRow row = dt.NewRow();
                    row["FileName"] = file.Name;
                    row["LastModifiedDate"] = file.LastWriteTime.ToString();
                    dt.Rows.Add(row);
                }
                dataGridView1.DataSource = dt;
            }
        }

If you would only like to display information about certain types of files, you can modify the parameter of the GetFiles method call appropriately.  In the following example, the code will only display information about  .pdf files contained in the selected directory:

foreach (FileInfo file in dir.GetFiles("*.pdf"))

Yes, I know...this example provides too much code to demonstrate how easy it is to programmatically obtain info about files in a given directory, but I thought it would be fun to add the additional code that could turn this into a nice utility if needed...