Archive for the ‘SharePoint 2010’ Category

Beware Long Fields In CAML Queries – Must Use Truncated Name

Tuesday, December 27th, 2011

Well, this is a new one for me. Tonight while making an application page I came across a frustrating thing. So here’s the scenario. I have list 1 called “TheParentList”, and no that’s not the real name. I have a second list called “theChildList”, again, not the real name.

My lookup column internal name, as a site column, is “Link_x0200_to_x0200_TheParentList_x0200_List_instructions

My CAML query is pretty straight forward and looks like this:

<Where><Eq><FieldRef Name=’Link_x0200_to_x0200_TheParentList_x0200_List_instructions’ LookupId=’TRUE’><Value Type=”Lookup’>4</Value></Eq></Where>

Further down the page I am displaying this field. So, theitems[“Link_x0200_to_x0200_TheParentList_x0200_List_instructions”].ToString();

Can you guess what happens? If I comment out my query, the page loads and the field is displayed just fine. If I try to use my query I get the “One or more field types are not installed properly”. So, after beating my head for hours I thought, well, when I click on the column from within the LIST and NOT from within the site column, I get a different name. I get Link_x0200_to_x0200_TheParentList_x0200_Li.

Notice the truncated name?? Well, if you try to use that truncated name in your call to display the field, theitems[“”].ToSTring(), it will fail because that’s not really the internal name.

However, apparently, when using a CAML query, the internal name of the field is actually the truncated value and NOT the full value. Crazy.

This will only happen with really long field names, which is why I suspect there isn’t much out there on it.

Hope this helps someone one day!

 

Passed MCPD 70-576 for SharePoint 2010!

Wednesday, November 23rd, 2011

Super excited to have passed my second SharePoint certification. I’m putting myself on a goal to at least qualify for the MCM cert. Of course, that requires a week in Redmond, and 19k check to Microsoft. Not sure the company will pay for it but hey, I can try.

I have already download the next measure up practice test. Less than six hours after passing 70-576 i’m already studying for the next one. Redmond here I come!

Site Collection Publishing Infrastructure And Term Store – How to delete leftover Term Store Groups

Monday, November 7th, 2011

If you have worked with the term store and site collections with the publishing infrastructure turned on in SharePoint 2010, you may have run into the issue of term store groups not being deleted. The code snippet below will take care of that for you but adding an event receiver to the site collection that will remove any associated automatically generated term store groups.

Here’s the scenario. You have created a new site collection, turned on publishing infrastructure but for some reason or another you need to delete it. So you delete the site collection, add it back again, (and this is the key part) with the same name, and turn on the publishing infrastructure again.

The users then say they  want to make use of the managed metadata term store but only allow each site collection to see its own group of term sets. No problem, you tell them. SharePoint creates a term store group for every site automatically when the publishing infrastructure is turned on. Everyone is happy until you get back to your desk and realize the term store group isn’t there.

Doing some research you find out that when you delete a site that had the publishing infrastructure feature activated SharePoint does not delete the term store group that got automatically created. So if you try to create another site with the same name, you won’t get your term store group created by the infrastructure. This is because another group in the term store has the same name. These groups are named using the site collection name. So the old term store group is still there as it was named after a site collection that had the same name as your current one.

Worse yet, you won’t be able to find this group in central admin. It’s only available to the site collection that it was paired with. That pairing is done based on the site GUID and not the site name. So when you delete the site, the connection is broken and the term store group is lost forever. The connection will not be reestablished when you create a new site collection with the same name.

To combat this, I created a little event receiver that will go into the managed metadata service and delete any groups that are named with the site collection name and the pattern used by the publishing infrastructure when it creates these groups.

Hope this helps someone one day. Enjoy.

  public override void SiteDeleting(SPWebEventProperties properties)

       {

           base.SiteDeleting(properties);

           SPSecurity.RunWithElevatedPrivileges(delegate()

           {

               SPSite site = properties.Web.Site as SPSite;

               TaxonomySession session = new TaxonomySession(site);

               string defaultName = “Site Collection – ” + (site.Url.Replace(“http://”, “”)).Replace(“https://”, “”).Replace(“/”, “-“).Replace(“:”, “-“);

               if (session.TermStores.Count > 0)

               {

                   TermStore store = session.TermStores[“Managed Metadata Service”];

                   foreach (Group thegroups in store.Groups)

                   {

                       if (thegroups.Name.ToString() == defaultName)

                       {

                           foreach (TermSet termsets in thegroups.TermSets)

                           {

                               termsets.Delete();

                           }

                           thegroups.Delete();

                       }

                   }

                   store.CommitAll();

               }

           });

       }

A glossary for SharePoint 2010 –

Wednesday, March 16th, 2011

I’m not copying the table on this one, here’s the link: http://msdn.microsoft.com/en-us/library/ee556558.aspx

List of tokens in SharePoint 2010

Monday, March 14th, 2011

Here is a handy table I found here http://msdn.microsoft.com/en-us/library/ee231545.aspx explaining what tokens are and which ones there are. I know I can just post hte link but including the table makes it more handy 🙂

Name Description
$SharePoint.Project.FileName$ The name of the containing project file, such as, “NewProj.csproj”.
$SharePoint.Project.FileNameWithoutExtension$ The name of the containing project file without the file name extension. For example, “NewProj”.
$SharePoint.Project.AssemblyFullName$ The display name (strong name) of the containing project’s output assembly.
$SharePoint.Project.AssemblyFileName$ The name of the containing project’s output assembly.
$SharePoint.Project.AssemblyFileNameWithoutExtension$ The name of the containing project’s output assembly, without the file name extension.
$SharePoint.Project.AssemblyPublicKeyToken$ The public key token of the containing project’s output assembly, converted to a string. (16-characters in “x2” hexadecimal format.)
$SharePoint.Package.Name$ The name of the containing package.
$SharePoint.Package.FileName$ The name of the containing package’s definition file.
$SharePoint.Package.FileNameWithoutExtension$ The name (without extension) of the containing package’s definition file.
$SharePoint.Package.Id$ The SharePoint ID for the containing package. If a feature is used in more than one package, then this value will change.
$SharePoint.Feature.FileName$ The name of the definition file of the containing feature, such as Feature1.feature.
$SharePoint.Feature.FileNameWithoutExtension$ The name of the feature definition file, without the file name extension.
$SharePoint.Feature.DeploymentPath$ The name of the folder that contains the feature in the package. This token equates to the “Deployment Path” property in the Feature Designer. An example value is, “Project1_Feature1”.
$SharePoint.Feature.Id$ The SharePoint ID of the containing feature. This token, as with all feature-level tokens, can be used only by files included in a package via a feature, not added directly to a package outside of a feature.
$SharePoint.ProjectItem.Name$ The name of the project item (not its file name), as obtained from ISharePointProjectItem.Name.
$SharePoint.Type.<GUID>.AssemblyQualifiedName$ The assembly qualified name of the type matching the GUID of the token. The format of the GUID is lowercase and corresponds to the Guid.ToString(“D”) format (that is, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
$SharePoint.Type.<GUID>.FullName$ The full name of the type matching the GUID in the token. The format of the GUID is lowercase and corresponds to the Guid.ToString(“D”) format (that is, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

InfoPath 2010 – SPContexts, code behind and sandboxed solutions

Monday, March 14th, 2011

I was recently tasked with adding some code behind to an InfoPath 2010 form and having it submit to multiple SharePoint lists. The issue I ran into was trying to get spcontext to work in a sandboxed solution. I couldn’t. Try as hard as I could, the spcontext was not being passed. I was getting the error that the object was not set to the instance of the object. Bottom line, when using Infopath 2010 in sandboxed mode, with security level of domain, you do not have access to the SPCONTEXT object. So everytime I tried to call it, it wouldn’t be called and the value was being set to Null.

The first solution is to set your InfoPath form to full trust and have an administrator approve it. But, I didn’t want to do that. All I wanted was to get the URL of the site. SO I did something neat. I found the SaveLocation that InfoPath stores and pulled out the URL from that.

 string strPath = e.InputParameters[“SaveLocation”].ToString();
            string strLoc = strPath.Substring(0, strPath.LastIndexOf(“/”));

            XPathNavigator mainNav = this.CreateNavigator();
            //this line will set a value for the field

            mainNav.SelectSingleNode(“//my:SiteUrl”, NamespaceManager).SetValue(strLoc.Substring(0, strLoc.LastIndexOf(“/”)).ToString());
            
           Then I just use my form variable in my SPSite declaration and i’m good to go.

using (SPSite site = new SPSite(MainDataSource.CreateNavigator().SelectSingleNode(“//my:SiteUrl”, NamespaceManager).Value))

If anyone can tell me why SPcontext isn’t available in the security level of Domain Trust in an InfoPath form, i would love to hear it.

More on adding to ULS logs

Wednesday, February 9th, 2011

Add this to a class, or anywhere accessible, and notes can be added to ULS logs to help with coding.

public static void AddNoteToULS(string thetitle, string themessage)
{
SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
diagSvc.WriteTrace(0, new SPDiagnosticsCategory(thetitle, TraceSeverity.Monitorable, EventSeverity.Error), TraceSeverity.Monitorable,
“Writing to the ULS log: {0}”, new object[] { themessage });
}

Quick way to add an error or not to ULS logs

Wednesday, February 9th, 2011

Here’s a quick neat snippet to add a comment to ULS logs. Helps to troubleshoot those hard errors.

using microsoft.sharepoint.administration;

try
{
//some code;
}
catch (Exception ex)
{
SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
diagSvc.WriteTrace(0, new SPDiagnosticsCategory(“EMACS CUSTOM”, TraceSeverity.Monitorable, EventSeverity.Error), TraceSeverity.Monitorable,
“Writing to the ULS log: {0}”, new object[] { ex.ToString() });
}

Tree view of all users on a site

Wednesday, January 19th, 2011

Here is a nice little web part I made to show all users and groups in a site and all subsites of a site collection. You can of course modify this to show only those users and groups from the current site and down but I elected to show from the parent site and down.

public class THISCLASS : WebPart
{
protected override void CreateChildControls()
{
try
{

SPSite spSite = SPContext.Current.Site;
SPSecurity.RunWithElevatedPrivileges(delegate()
{

// set the tree view properties
TreeView treeView = new System.Web.UI.WebControls.TreeView();
treeView.ShowLines = true; // show lines
treeView.ExpandDepth = 1;

using (SPSite oSite = new SPSite(spSite.Url))
{

//SPWebCollection collWebsites = oSite.AllWebs;
SPWeb thew = oSite.RootWeb;

TreeNode vsNode = new TreeNode(thew.Title.ToString(), null, null, thew.Url, “_self”);
treeView.Nodes.Add(vsNode);
LoadGroupsAndUsers(thew, vsNode);

foreach (SPWeb siteCol in thew.Webs)
{
TreeNode siteColNode = new TreeNode(siteCol.Title.ToString(), null, null, siteCol.Url, “_self”);
vsNode.ChildNodes.Add(siteColNode);
LoadGroupsAndUsers(siteCol, siteColNode);
LoadTreeViewForSubWebs(siteCol, siteColNode);
siteCol.Dispose();
}
}

this.Controls.Add(treeView);
});
}
catch (Exception e)
{

HtmlTable table = new HtmlTable();
HtmlTableRow row = new HtmlTableRow();
table.Rows.Add(row);
HtmlTableCell cell1 = new HtmlTableCell();
row.Cells.Add(cell1);
Label thelabel = new Label();
thelabel.Text = (e.ToString());
cell1.Controls.Add(thelabel);
this.Controls.Add(table);
}
// this.Controls.Add(node);
}

private void LoadTreeViewForSubWebs(SPWeb currentWeb, TreeNode currentNode)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
foreach (SPWeb web in currentWeb.Webs)
{
TreeNode webNode = new TreeNode(web.Title.ToString(), null, null, web.Url, “_self”);
currentNode.ChildNodes.Add(webNode);

LoadGroupsAndUsers(web, webNode);

if (web.Webs != null && web.Webs.Count > 0)
{
LoadTreeViewForSubWebs(web, webNode);
web.Dispose();
}
}
});
}

private void LoadGroupsAndUsers(SPWeb currentWeb, TreeNode currentNode)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
foreach (SPGroup thegroups in currentWeb.Groups)
{
TreeNode gnode = new TreeNode(thegroups.Name.ToString());
currentNode.ChildNodes.Add(gnode);

foreach (SPUser theusers in thegroups.Users)
{
//string theurl = null;
string theurl = currentWeb.Url + “/_layouts/userdisp.aspx?” + theusers.ID.ToString();
TreeNode theuser = new TreeNode(theusers.Name.ToString(), null, null, theurl,”_self”);
gnode.ChildNodes.Add(theuser);
}
}
});
}

}