How to redirect a user to a custom page after adding a new List Item

This took me a while to figure out how to do this so I thought I’d share my experience.

Problem:
User needs to perform additional tasks after a new list item has been added to a list. I my case, I wanted the user to create a new list item of information that needed to be emailed to a certain group of people, based on what he/she selected in the new list item form.

Solution:
Create an Event Handler for the list and overriding the ‘ItemAdding’ method to:
(more…)

How to hide a SharePoint list column from a list form (New, Edit, and Display)

Managing the Content Type of a SharePoint List will provide you with the option to Show or Hide a column (aka Field) from the “New.aspx”, “Edit.aspx”, and “Display.aspx” list forms. If you didn’t want to show a column on any of the forms, you could select the column and ‘hide’ it from all forms.

Unfortunately, if you manage the content type of a list form, you are not able to choose which of these forms you can hide a column from. By default, hiding a column will be hidden on all forms.

In this post, I will show you how you can view all columns in your list and then choose which form to hide the column from.

(more…)

How to get all ‘Inherited Security’ sites in your SharePoint site collection

This article is nothing new to SharePoint developers. In fact, there are better options of getting a listing of all subsites in your site collection. However, for those who are not experienced developers, this article will give you an idea of what you can do with the SharePoint web services.

(more…)

How to add a SharePoint List field programmatically

You probably won’t have any reason to do this outside of the SharePoint UI, but it can be helpful if you have a number of lists you want to add a column (field) to. This little piece of code can come in handy.

(more…)

Code Snippet: Delegating elevated priviledges to your SharePoint method


try
{
	SPSecurity.RunWithElevatedPrivileges(delegate()
	{
		using (SPSite site = new SPSite(properties.WebUrl))
		{
			using (SPWeb web = site.OpenWeb())
			{
				//do code
			}
		}
	});
}
catch (Exception ex)
{
	//
}

(more…)

When to use SystemUpdate when editing a SharePoint list item programmatically


When coding against the SharePoint object model, I will generally do the following:

  string newFolderName = "My new folder name";
  folder.Item[SPBuiltInFieldId.Title] = newFolderName;
  folder.Item.Update(); // or SystemUpdate(false)

(more…)

Adding an attachment to a SharePoint list item programmatically

I needed a way to upload an attachment to a list item within an Event Handler but discovered it was not that simple. Now that I have the code to add attachment to a list item, I don’t want to lose it.

The following code can be called from a web part or an application page easily. What was complicated for me was trying to attach a file from the ItemAdding/ItemAdded Event Handler.
(more…)

Code Snippet: Using the List Field Iterator to display a list form in a SharePoint Web Part

You can use the list field iterator to show the ‘New’, ‘Edit’, and ‘Display’ form in a web part. The List Field Iterator will show all fields in your list view that you specify, and inherits the properties you specify for the view as well (such as column ordering and visible fields).

 
(more…)

Code Snippet: How to read and modify an XML file programmatically

STEP 1: Create an XML file on your local machine 

<?xml version="1.0" encoding="utf-8"?>
<Address>
  <Address1>One Microsoft Way</Address1>
  <City>Redmond</City>
  <State>WA</State>
  <Zip>98035</Zip>
</Address>

 
(more…)

Code Snippet: How to read an XML file from a web site URL

private void button1_Click(object sender, EventArgs e)
{
    string XML_CONFIGURATION_FILE = "http://somewebserverurl/somefile.xml";
  
    Uri uri = new Uri(XML_CONFIGURATION_FILE);
    WebRequest req = WebRequest.Create(uri);
    req.UseDefaultCredentials = true;
    WebResponse response = req.GetResponse();
    Stream stream = response.GetResponseStream();
  
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(stream);
  
    XPathNavigator xNav = xDoc.CreateNavigator();
  
    tbDetails.Text += xNav.SelectSingleNode("/XMLNode/ChildNode/Element").Value.ToString();
}

 
(more…)