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("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!", 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; } } {0}
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:
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:
- Open IE and navigate to the SharePoint site where your forms library exists
- Click on Site Actions -> View All Site Content
- Under the Document Libraries section, locate and click on the link for your form library
- On the Library Tools tab, click on Library
- Locate the Connect & Export section and click on Open with Explorer
- When Windows Explorer pops up, click on the Forms directory
- Locate the "template.xsn" file
- 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):
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...
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...
Thursday, January 31, 2013
List of "illegal" characters that cannot be included in a SharePoint file name
I've been working on a utility that our team is going to leverage for rapidly uploading a ton of existing Policy and Procedure documents from file shares into a SharePoint site (by the way, this includes defining the relevant document metadata which is why we aren't simply copying the files over). During my investigation, I happened to notice that many of the file names in the file share contain characters that SharePoint is not going to like at all (i.e. '&' is the leading offender). In case you're curious what these "illegal" characters are for file names, here is a complete list:
The source of this information is the following link: http://support.microsoft.com/kb/905231
If you're looking for a solution to this problem, one option is to use the code that I created in one of my previous posts that will allow you to rename the file as you're adding to to your document library: Remove special characters from a string using Regular Expression
- Tilde
- Number sign
- Percent
- Ampersand
- Asterisk
- Braces
- Backslash
- Colon
- Angle brackets
- Question mark
- Slash
- Pipe
- Quotation mark
The source of this information is the following link: http://support.microsoft.com/kb/905231
If you're looking for a solution to this problem, one option is to use the code that I created in one of my previous posts that will allow you to rename the file as you're adding to to your document library: Remove special characters from a string using Regular Expression
Thursday, January 24, 2013
How to find SharePoint 2013 Central Administration
I know that I'll be laughing about this post in the future, but I've got to be perfectly honest and tell you that it took me nearly 20 minutes to figure out how to access SharePoint 2013 Central Administration in my new Windows 2012 Server environment. Here two different methods that I finally stumbled upon ways that will make this happen:
Method 1:
Method 1:
- Log on to your Windows 2012 Server environment
- Click on the center of the screen
- Move your mouse to the upper right corner of the screen and a panel will appear on the far right side of the screen
- Click on the Search icon
- Locate the Microsoft SharePoint 2013 section
- Click on the SharePoint 2013 Central Administration icon
Method 2:
- Log on to your Windows 2012 Server environment
- Click on the center of the screen
- Move your mouse to the upper right corner of the screen and a panel will appear on the far right side of the screen
- Click on the Start icon
- Move your mouse to the bottom of the screen and right click
- Click on the All Apps icon
- Locate the Microsoft SharePoint 2013 section
- Click on the SharePoint 2013 Central Administration icon
Tuesday, January 22, 2013
Programmatically obtain a list of SharePoint Web Applications located under a SharePoint Farm
If you ever need to programmatically obtain a list of web applications contained under your SharePoint Farm, here is some code that will do just that...
NOTE: In this particular instance, I'm writing my list of web applications to a combo box control of a Windows application. The idea is that this combo box control will be the first of several chained combo boxes that will allow you to dive deeper into the SharePoint objects based on the items selected in the combo box controls (i.e. Web Application -> Site Collection -> Site -> Document Library -> Document). Perhaps this my inspire some ideas of how or why this code would be useful.
SPSecurity.RunWithElevatedPrivileges(delegate() { SPFarm farm = SPFarm.Local; SPWebService service = farm.Services.GetValue(""); foreach (SPWebApplication webApp in service.WebApplications) { // The following code will need to be changed depending on your situation ComboBoxItem webAppItem = new ComboBoxItem(webApp.DisplayName, webApp.Id); cbWebApp.Items.Add(webAppItem); } });
NOTE: In this particular instance, I'm writing my list of web applications to a combo box control of a Windows application. The idea is that this combo box control will be the first of several chained combo boxes that will allow you to dive deeper into the SharePoint objects based on the items selected in the combo box controls (i.e. Web Application -> Site Collection -> Site -> Document Library -> Document). Perhaps this my inspire some ideas of how or why this code would be useful.
Tuesday, January 15, 2013
A solution for providing a "sum" operation on a calculated field in a SharePoint view
One of our site owners recently ran into a situation in which he was trying to create a SharePoint view that would provide a sum of all values contained within a calculated field, but was not able to do so via the simple configuration process. By "simple configuration" process, I'm referring to the typical way that you would provide a sum on a standard number or currency field which would be to place your view in edit mode, scroll down to and then expand the Totals group, locate your field, select the "Sum" operation listed in the drop down control, and save the change to your view. Unfortunately, this isn't possible for calculated fields due to the fact that calculated fields don't show up under the Totals group. With this in mind, a few possible solutions that would provide the requested functionality are listed as follows:
After a discussion with the user about the requirements for this effort as well as the various options available, we came to the conclusion that it would be possible to use a simple, one-time solution to meet the needs of his effort. In light of this, we chose the workflow option and decided to add our workflow to the list using SharePoint Designer. If you're interested in this solution, here are the steps we took to make this happen:
Replace the calculated field with a standard number or currency field
- Use a Content Query Web Part to host the XSLT code that would generate the desired view
- Use SharePoint Designer to modify the XSLT code of the view directly. In SharePoint 2007, you would do this by inserting a Data View web part and modifying the XLST of that web part.
- Change the calculated field to a regular number or currency field, add a workflow to the list that would perform the calculations previously associated with the calculated field, and place a sum operation on the field
After a discussion with the user about the requirements for this effort as well as the various options available, we came to the conclusion that it would be possible to use a simple, one-time solution to meet the needs of his effort. In light of this, we chose the workflow option and decided to add our workflow to the list using SharePoint Designer. If you're interested in this solution, here are the steps we took to make this happen:
Replace the calculated field with a standard number or currency field
- Using your browser, open the site and navigate to the list to be modified
- Under List Tools, click on the List tab
- Click on List Settings
- Under Columns, locate your calculated field and click on it's link
- Record the formula for your calculation
- Click Delete to remove the column
- Click on Create column
- For Column name:, enter the name of the original calculated field that you just deleted
- Select either the Number or Currency radio button depending on which you require
- Click OK
Create and publish the calculation workflow using SharePoint Designer
- Open Microsoft SharePoint Designer 2010
- Click Open Site
- For Site Name:, enter the URL of the site your list is contained under
- Click Open
- Under Navigation, click on Lists and Libraries
- Click on the list you wish to perform the calculations on
- Under Workflows, click on the New... icon
- Add an appropriate Title and Description for you workflow
- Click OK
- Once the Workflow Designer appears, click on the Action button
- Under Core Actions, select Do Calculation
- Click on the first value link
- Click on the function icon
- For Data source:, select Current Item
- For Field from source:, select your first field that is part of the calculation
- Click OK
- Click on the plus link and choose the appropriate option for your calculation
- Click on the second value link
- Click on the function icon
- For Data source:, select Current Item
- For Field from source:, select your second field that is part of the calculation
- Click OK
- Click on the Action button
- Scroll down to List Actions and click on Set Field in Current Item
- Click on the field link and select the field you just created previously that replaced the original, calculated field
- Click on the value link
- Click on the function icon
- For Data source:, select Workflow Variables and Parameters
- For Field from source:, select the Variable: calc item
- Click OK
- If needed, continue adding calculation steps until the workflow is complete
- Click the Save button
- Under Navigation, click on Workflows
- Click on the link associated with your newly created workflow
- Under Start Options, remove the check from the Allow this workflow to be manually started option
- Place a check in both the Start workflow automatically when an item is created and Start workflow automatically when an item is changed options
- Click the Save button
- Click the Publish button
Update your view to provide a sum on the new field
- Using your browser, open the site and navigate to the list to be modified
- Under List Tools, click on the List tab
- Click on List Settings
- Under Views, locate your view and click on it's link
- Scroll down to and then expand the Totals group
- Locate your new field and select the "Sum" operation listed in the drop down control
- Click Save
With these steps complete, the new "calculated" field will be updated by the workflow every time a list item is created or modified. NOTE: Users will be able to enter values into the "calculated" field; however, the workflow will ultimately override this value when the items is created or saved. At this point, you will now have a "sum" operation on your "calculated" field.
Subscribe to:
Posts (Atom)