Learn how to use SPQuery object to fetch data from a SharePoint list. In one of the previous articles, you saw how one can populate a drop down list box without writing code but now you will see how to do it programmatically. Programming means more power in your hands!
All About SharePoint is a blog about SharePoint, InfoPath and related technologies. It was started in 2005 and has good readership in many countries. It covers SharePoint 2003, 2007, 2010, 2013, 2016 and Office 365. You can read articles, tips, tutorials and news. It has code samples and complete applications as well.
Saturday, July 12, 2008
Wednesday, July 9, 2008
InfoPath: Raising an error on validation
InfoPath provides excellent method to validate controls, for example, you can use data validation alone or in conjuction with Rules to validate data in InfoPath forms. You can also do it programmatically and raise errors which will be shown in the form when there is a validation error. Assume there is a control called FirstName. Here is how you will raise an error on FirstName:
public void FirstName_Changed(object sender, XmlEventArgs e)
{
}
Right click the control and select Programming > Changed Event. Note, Validating Event is also available but we will use Changed Event. Raise any previously raised errors:
this.Errors.Delete("FirstNameError");
After validating, if there is an error, raise it:
this.Errors.Add(Nav, "FirstNameError", "Please add correct value in First Name");
The first parameter is the XPathNavigator object that points to the control.
XPathNavigator nav = this.CreateNavigator().SelectSingleNode(XPath of the firstname field, this.NamespaceManager);
XPath will be like "/my:myFields/my:FirstName".
The second parameter is the name of the error and third parameter is the description of the error.
Happy programming!
Wednesday, June 18, 2008
Populating InfoPath Drop down with SharePoint List Data
Hard coding values in InfoPath drop down list box is easy but you can also populate it with dynamic data. Data can be from an XML file, a database or even a SharePoint list. This article shows how you can populate the list box with data from a SharePoint list with out writing a line of code.
Friday, June 6, 2008
InfoPath: Reading from a repeating table
I will start with the code first and then explain what's happening.
Code:
XPathNavigator DOM = this.MainDataSource.CreateNavigator();
XPathNodeIterator nodes = DOM.Select("/my:myFields/my:RepeatingGroup", this.NamespaceManager);
XPathNavigator nodesNavigator = nodes.Current;
XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Element, false);
XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Element, false);
string FirstName = "";
string LastName = "";
while (nodesText.MoveNext())
{
if (nodesText.Current.Name == "my:FirstName")
{
FirstName = nodesText.Current.Value.ToString();
}
{
if (nodesText.Current.Name == "my:FirstName")
{
FirstName = nodesText.Current.Value.ToString();
}
nodesText.MoveNext();
if (nodesText.Current.Name == "my:LastName")
{
LastName= nodesText.Current.Value.ToString();
}
{
LastName= nodesText.Current.Value.ToString();
}
}
nodesText = null;
nodesNavigator = null;
nodes = null;
DOM = null;
nodesNavigator = null;
nodes = null;
DOM = null;
Explanation:
It's a very commong scenario. Developers want to read from the repeating table programmatically. The above code just does that. You take the XPath for the repeating group and create a nodes iterator object. This will iterate through all the descendant nodes. If the node type (XPathNodeType) is "Element", the value will be read into a string variable. That's it. Use MoveNext() to move to the next node in the hierarchy. It's as simple as that!
Wednesday, May 14, 2008
XPath, XQuery, and XSLT Functions
Some XML functions can be very useful in InfoPath, especially if you are an InfoPath developer. For example, suppose you are using data validation in your InfoPath form and you want the users to enter a value in one of the text boxes on your form. Of course, you can use the built-in data validation functions, for example, "If field is blank then show error alert" but users can dodge you by entering a space in the text box. In this case, InfoPath won't generate any error and will accept the value entered by the user. How can handle this situation? The answer is XML functions. You can use normalize-space() function. Right click the control and select "Data Validation". In the drop down, select "The expression" and in the text box enter normalize-space(.)="". In the screen tip, enter "Please enter valid value" and click Ok. InfoPath will now show an error if users try to enter an invalid value like a whitespace. Similarly, you can use other XML functions in your code or to validate data using the built-in data validation. More functions can be found on the following page. The page has the list of functions and examples to show how each function works:
-SSA
Tuesday, May 6, 2008
How to grant permissions to a custom assembly that is referenced in a report in Reporting Services
It seems straight forward. Isn't it? but it may not be as simple and straight forward for the newbies as it seems at first. A colleague and a friend of mine recently was struggling with the same issue and he learnt the lesson after spending considerable amount of time experimenting with different solutions. You get a security exception when you write your own component to be used in a report in reporting services. My friend is working on a SharePoint project that involves reporting services. He created a report that used a custom built component. The code used SharePoint object model. He tried to use Elevated privileges but even that didn't work because the error is thrown on the line that has the elevated priviliges code. He even tried the code access security, that didn't work. The following KB article has information about this problem:
http://support.microsoft.com/kb/842419/ (How to grant permissions to a custom assembly that is referenced in a report in Reporting Services)
Use following code to get rid of the security exception:
SharePointPermission perm = new SharePointPermission(PermissionState.Unrestricted);
perm.Assert();
perm.Assert();
//Code using SharePoint object model here!!
perm.Deny();
Include following DLL in the references before you use the above code:
Microsoft.SharePoint.Security.DLL
This DLL will be found in the 12 hive. Exact location is as following:
System Drive:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI
Add following namespaces at the top:
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
That's it. Your component will now work fine.
Monday, May 5, 2008
Creating SharePoint List Views Programmatically
Learn how to create SharePoint list views programmatically.
Subscribe to:
Comments (Atom)