Sunday, January 29, 2012

SharePoint PowerShell Error: The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered.

Sometimes when you try to run a SharePoint 2010 Management Shell (PowerShell), you might encounter the error below.

The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered.

clip_image002[8]

Although, the answer is not obvious given the error message, the answer is quite simple.  You tried to run PowerShell with a user account that is not an administrator in SharePoint.  In this case, I had ran it with my personal account which is not an admin and does not have access to the underlying SharePoint databases.  Hopefully, you used a “setup” account when you created SharePoint.  Execute PowerShell with that account instead. You can do this either by logging in as that user. You can also hold down the Shift key and right click on the SharePoint 2010 Management Shell icon and choose the Run as different user menu item.

clip_image004[4]

If you don’t have a setup account, things might be a bit trickier.  You can also resolve this by running it as the farm account, but you really want to try and avoid this. You can refer for the service accounts for SP2010 in MSDN for details of different service accounts.

Thanks for reading. If you have some other explanation – please post a comment… I’ll be happy to hear.

...HaPpY CoDiNg

Partha (Aurum)

References:

 

Tuesday, November 22, 2011

SPD Custom Changes with JavaScript

  

This article deals with the SharePoint designer and displays how to add custom java scripts to add new functionalities and UI changes.

 

Once we have our custom page ready we can do some experiments with javascript to change the look and feel. In this post I will show two changes made to a list page.

1.       Colour coding few sections

2.       changing the text of the field name

 

Changes would be made to a list page Newform.aspx of TestJSForm list. We are going to colour code the NewForm.aspx. Fields below 2; between 2-3 and greater than 3 would have three different colours. At the end column 2’s text would be changed.  

 

Once you open the page in the SPD go to the content place holder “PlaceHolderPageTitleInTitleArea”. We are going to write our javascript code inside this content place holder.  See below the code stub.

 

<asp:Content ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">

                <SharePoint:ListProperty Property="LinkTitle" runat="server" id="ID_LinkTitle"/>:

                <SharePoint:ListItemProperty id="ID_ItemProperty" MaxLength=40 runat="server"/>

                <script type="text/javascript" language="javascript">

                _spBodyOnLoadFunctionNames.push('__colorCodeForm');

                function __colorCodeForm()

                {             

                                //alert('Hello world');

Var ParentChildID = document.getElementById('part1').childNodes[2].childNodes[0].childNodes;

//alert (ParentChildID.nodeNames); //alert (ParentChildID.length);

for (i=0; i<ParentChildID.length; i++)

{

                if (i<=2)

                {

                                //ParentChildID[i].childNodes[0].style.backgroundColor = '#ebf3cc';

                                ParentChildID[i].childNodes[1].style.backgroundColor = '#CCFFFF';

                }

                if (i > 2 && i<=3)

                {

                                //ParentChildID[i].childNodes[0].style.backgroundColor = '#ebf3cc';

                                ParentChildID[i].childNodes[1].style.backgroundColor = '#FFFF99';

                }

                if (i > 3)

                {

                                //ParentChildID[i].childNodes[0].style.backgroundColor = '#ebf3cc';

                                ParentChildID[i].childNodes[1].style.backgroundColor = '#FFCC99';

                }

}

//get to the item and change the innertext and html

                var item=ParentChildID[1].childNodes[0];

                //alert(item.innerText);               

                item.innerText="Partha";

                var item2=ParentChildID[1].childNodes[1].childNodes[1];

                //alert(item2.innerHTML);

                var item3=ParentChildID[1].childNodes[1];

                item3.innerHTML = item2.innerHTML + "This Description is being created by JS";

}

</script>

 

 

 

The line document.getElementById('part1').childNodes[2].childNodes[0].childNodes; is very important. This is the node which opens the nested child controls. The logic behind is when the listformview webpart is populated in the browser it get rendered as

<td valign="top"><div WebPartID="c9128cf3-3183-4bf7-8eb5-250fae8ae693" HasPers="false" id="WebPartWPQ2" width="100%" allowDelete="false" style="" >

<SPAN id='part1'>

<table class="ms-formtoolbar" cellpadding="2" cellspacing="0" border="0" id="ctl00_m_g_c9128cf3_3183_4bf7_8eb5_250fae8ae693_ctl00_toolBarTbltop" width="100%" >

  <tr>

 

The JS function traverses the DOM to get to the Span with id part1. Rest all the JS functions would be based on this logic only.

 

Another very important and interesting JS, which gets the element based on the tag name.

<script type="text/javascript">

//this function gets the element with tag name

function getTagFromIdentifierAndTitle(tagName, identifier, title) {

  var len = identifier.length;

  var tags = document.getElementsByTagName(tagName);

  for (var i=0; i < tags.length; i++) {

    var tempString = tags[i].id;

    if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {

      return tags[i];

    }

  }

  return null;

}

</script>

</asp:Content>

 

The final output will be something similar to

clip_image002[8]

 

 

 Thanks for reading. If you have some other explanation – please post a comment… I’ll be happy to hear.

...HaPpY CoDiNg

Partha (Aurum)

References:

http://office.microsoft.com/en-us/sharepoint-designer-help/CL010082351.aspx?CTT=97

http://office.microsoft.com/en-us/sharepoint-designer-help/create-a-custom-list-form-HA010119111.aspx

 

SPD to Hide list field in NewForm.aspx

 

Hiding few columns from the default view DataFormWebPart webpart.

 

This article would cover the we can change the new from page of any list. Generally we get to see the new form page with application master page, but here I have changed it to default master page.

Additionally we will also see how to hide even mandatory field. However,  except for the title field SharePoint does not enter any default value.

 

I would suggest to add a new page to the list to show  dataformwebpart and then do changes. I won’t go in detail of how to add pages, rather follow the link below to create/know how to add list page in any list from SPD please refer to the below link.

http://office.microsoft.com/en-us/sharepoint-designer-help/create-a-custom-list-form-HA010119111.aspx

 

The SPD code change would look like below. I am not adding whole of the Placeholdermain content place holder but only the html table where the fields are there.

<table border="0" cellspacing="0" width="100%">

<tr><td colspan="2" style="background-color:olive; font-family:Verdana;font-weight:bold; text-align:center">Test of SPD List Page</td></tr>

<tr><td width="190px" valign="top" class="ms-formlabel">

                <H3 class="ms-standardheader">

                <nobr>Title<span class="ms-formvalidation"> *</span>

                </nobr></H3></td>

<td width="400px" valign="top" class="ms-formbody">

<SharePoint:FormField runat="server" id="ff1{$Pos}" ControlMode="New" FieldName="Title" __designer:bind="{ddwrt:DataBind('i',concat('ff1',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Title')}"/>

<SharePoint:FieldDescription runat="server" id="ff1description{$Pos}" FieldName="Title" ControlMode="New"/>

</td>

</tr>

<tr><td width="190px" valign="top" class="ms-formlabel">

                <H3 class="ms-standardheader">

                <nobr>Col1</nobr>

                </H3></td>

<td width="400px" valign="top" class="ms-formbody">

<SharePoint:FormField runat="server" id="ff2{$Pos}" ControlMode="New" FieldName="_x0043_ol1" __designer:bind="{ddwrt:DataBind('i',concat('ff2',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@_x0043_ol1')}"/>

<SharePoint:FieldDescription runat="server" id="ff2description{$Pos}" FieldName="_x0043_ol1" ControlMode="New"/>

</td></tr>

 

<!--<tr>                <td width="190px" valign="top" class="ms-formlabel">

                <H3 class="ms-standardheader">

                <nobr>Col5 <span class="ms-formvalidation"> *</span></nobr>

                </H3></td>

                <td width="400px" valign="top" class="ms-formbody">

                <SharePoint:FormField runat="server" id="ff3{$Pos}" ControlMode="New" FieldName="_x0043_ol5" __designer:bind="{ddwrt:DataBind('i',concat('ff3',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@_x0043_ol5')}"/>

                <SharePoint:FieldDescription runat="server" id="ff3description{$Pos}" FieldName="_x0043_ol5" ControlMode="New"/>

                </td>

                </tr>-->

               

<tr><td width="190px" valign="top" class="ms-formlabel">

<H3 class="ms-standardheader">

<nobr>Col6</nobr>

</H3>

</td>

<td width="400px" valign="top" class="ms-formbody">

<SharePoint:FormField runat="server" id="ff4{$Pos}" ControlMode="New" FieldName="_x0043_ol6" __designer:bind="{ddwrt:DataBind('i',concat('ff4',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@_x0043_ol6')}"/>

<SharePoint:FieldDescription runat="server" id="ff4description{$Pos}" FieldName="_x0043_ol6" ControlMode="New"/>

</td></tr>

<xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">

<tr><td colspan="99" class="ms-vb">

<span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" ddwrt:ammode="view"></span>

</td></tr></xsl:if>

</table>

 

 

The final output would be something

clip_image002

 

 

Thanks for reading. If you have some other explanation – please post a comment… I’ll be happy to hear.

...HaPpY CoDiNg

Partha (Aurum)

References:

http://office.microsoft.com/en-us/sharepoint-designer-help/CL010082351.aspx?CTT=97

http://office.microsoft.com/en-us/sharepoint-designer-help/create-a-custom-list-form-HA010119111.aspx

 

Thursday, November 17, 2011

Trying to use SPWeb object that has been closed or disposed and is no longer valid.

Are you trying to do this:

using (SPWeb spweb = SPContext.Current.Web)
    {

     ....
    }

this will give an above stated exception.

You may never close the objects SPContext.Current.Web or SPContext.Current.Site because these are the instances your sharepoint is running on. On the other hand, if you create your own instances of SPWeb or SPSite, you must dispose of them.

using (SPSite spsite = new SPSite(SPContext.Current.site.url))
    {

     using (SPWeb spweb = spsite.openweb())
    {        ....
    }

  }

There is a tool developed by microsoft (SPDisposeCheck) that checks your disposing habits and gives you a nice report of possible issues: http://archive.msdn.microsoft.com/SPDisposeCheck

You can even see the best practices of Microsoft -There is

http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx

 

Thanks for reading. If you have some other explanation – please post a comment… I’ll be happy to hear.

...HaPpY CoDiNg

Partha (Aurum)

References:

http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx

 

Wednesday, November 16, 2011

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

 

I have faced this error on various instances; see below the scenarios and solutions.

 

1.       It seems the error will occur when you have no permission to update the item. Try to run your code in RunWithElevatedPrivileges as below:

SPSecurity.RunWithElevatedPrivileges(delegate()

{

 using (SPSite spSite = new SPSite("http://MyWebApp"))

 {

using (SPWeb spWeb = spSite.OpenWeb())

 {      // insert your code here  }   }});

 

2.       The While using RunWithElevatedPrivileges you got following error.
Error : Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack when use new spsite
Solution: SPSite and SPWeb object should be created inside delegate(), example below. If not you might get the above error.CSS

Wrong Code:

string siteUrl = @" http://MyWebApp"

SPSite site = null ;  

SPWeb web = null

SPSecurity.RunWithElevatedPrivileges(delegate() 

   site = new SPSite(siteUrl); 

   web = SPSDestSite.OpenWeb();       

    // To do ( run code here )       

}); 

web.Close();

site.Close(); 

 

Correct Code:

string siteUrl = @" http://MyWebApp"

SPSecurity.RunWithElevatedPrivileges(delegate() 

using (SPSite site = new SPSite(siteUrl)) 

{

    using (SPWeb web = SPSDestSite.OpenWeb())

    { 

           // To do ( run code here ) 

    } 

}); 

 

Thanks for reading. If you have some other explanation – please post a comment… I’ll be happy to hear.

...HaPpY CoDiNg

Partha (Aurum)

References:

http://sites.wizdim.com/pathikrawal/sharepoint-2010/errorunable-to-evaluate-expression-because-the-code-is-optimized-or-a-native-frame-is-on-top-of-the-call-stack-while-debugging-project-in-visual-studio-2010-for-sharepoint-2010