A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

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();
//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;
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.

Validating contact selector control programmatically

How to know if contact selector is empty?
XpathNavigator selector = this.CreateNavigator().SelectSingleNode("/my:myFields/my:contactselector", this.NamespaceManager);
if (selector.SelectChildren(XPathNodeType.Element).Count == 0)
{
this.Errors.Add(selector, "Error","Selector is empty");
}
How to raise error if user has selected more than one person in the selector?
if (selector.SelectChildren(XPathNodeType.Element).Count > 1)
{
this.Errors.Add(selector, "Error","Please select one user");
}
How to tell if user hasn't selected a valid person?
  selector = selector.SelectSingleNode("my:Person", this.NamespaceManager);
XPathNavigator displayname = this.CreateNavigator().SelectSingleNode("/my:myFields/my:contactselector/my:Person/my:DisplayName", this.NamespaceManager);
string strDisplayName = displayname.value.toString();
XPathNavigator accountid = this.CreateNavigator().SelectSingleNode("/my:myFields/my:contactselector/my:Person/my:AccountId", this.NamespaceManager);
string strAccountid = accountid.value.toString();
XPathNavigator accounttype = this.CreateNavigator().SelectSingleNode("/my:myFields/my:contactselector/my:Person/my:AccountType", this.NamespaceManager);
string strAccountType = accounttype.value.toString();

                    if (string.IsNullOrEmpty(strDisplayName)) ||
                        string.IsNullOrEmpty(strAccountId)) ||
                        string.IsNullOrEmpty(strAccountType))
                    {
                         this.Errors.Add(selector,"Error","Please select valid user");
                    }
Note: This was off the top of my head, you may find minor mistakes in XPaths or spellings.

Sunday, April 20, 2008

MVP Summit experience

Returned from the summit on friday night but my body was so sour that I spent whole saturday sleeping, relaxing, and watching TV. This was my first summit experience and undoubtedly it was the best experience I have had in recent times. This was the first time I met SharePoint MVPs in person. I have uploaded some pictures that I took during the summit, here are the URLs:
The sessions were very informative and I got chance to spend some quality time with the other MVPs during the events like Paintball, Attendee party, PG dinner, etc. I also got chance to meet with MVPs from around the world. All in all, it was an excellent experience and I really enjoyed the visit and I hope to visit the summit again next year which is expected to start on March 1st, 2009. Those of you interested in finding out how we SharePoint MVPs had fun at the summit may want to read the following blog post:

-Saif


Friday, April 18, 2008

Permissions error when adding data to a list (programmatically)

MSDN article does not mention this but the code smaple wont work if you copy it directly from the MSDN article. You get an error when you try to add a record in list. Here is the code:
SPSite site = SPContext.Current.Site;
SPWeb localweb = site.OpenWeb();
SPList list = localweb.Lists["User List"];
SPListItem listItem = list.Items.Add();
localweb.AllowUnsafeUpdates = true;
listItem["Title"] = "test";
listItem.Update();
localweb.AllowUnsafeUpdates = false;
This will not work. First thing, you should use elevated privileges to get rid of the permissions exception. Second thing, the SPContext should be used outside the elevated privilges code. Here is how to do it correctly:

SPWeb webroot = null;
try
{
webroot = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(webroot.Site.ID))
{
using (SPWeb localweb = site.OpenWeb(webroot.ID))
{
SPList list = localweb.Lists["User List"];
SPListItem listItem = list.Items.Add();
localweb.AllowUnsafeUpdates = true;
listItem["Title"] = fname.Value.ToString();
listItem.Update();
localweb.AllowUnsafeUpdates = false;
}
}
});
Instantiate the SPWeb object to null. Assign SPContext outside elevated privileges. Set AllowUnsafeUpdates to true before updating the item.

Thursday, April 10, 2008

Programming InfoPath Web Forms

InfoPath makes it easy to create electronic forms without coding. Programming InfoPath XML forms is fun. You can take full advantage of the new InfoPath forms services by creating solutions through coding. Programming gives you power and you can do several things that may not be possible without it. For example, creating a multi-select list box for browser enabled forms is one example. The out-of-the-box multi-select list box does not work in browser forms. You can create one using the out-of-the-box repeating table. This introductory level article does not cover those details but shows how you can create a programmatic solution using VSTA.

Tuesday, April 1, 2008

MVP Renewed!

My MVP award has been renewed for another year. I am very happy and very excited because I have lots of interesting plans this year. I would like to thank all my well wishers and readers who read my blog and articles and send positive feedback and comments. Thank you!

 -SSA