Thursday, January 17, 2013

Hiding SharePoint list field (column) from newform and editform using powershell - SharePoint 2010



Use the below power shell command to hide a particular  column in a sharepoint list from the newform and editform.



$WebUrl = "http://sitecollectionurl"
Write-Host "Opening Web" $WebUrl
$web = Get-SPWeb $WebUrl


$list = $web.Lists["ListName"];



$FieldGuid = New-Object System.Guid("cc9576b6-a166-47c9-bd89-7f47a3237e03");

$Field = $list.Fields[$FieldGuid];

$Field.ShowInNewForm = $false;
$Field.ShowInDisplayForm = $true;
$Field.ShowInEditForm = $false

$Field.Update();

$list.Update();



Here, replace the $WebUrl with your site collection URL, and replace the "ListName" with your SPList name.

And replace the Guid"cc9576b6-a166-47c9-bd89-7f47a3237e03" with the id of the field to be hidden.

For getting the id of the field , you can use 'SharePoint Manager' tool and will be downloaded from here. Its from codeplex and it is free.

Show property bag values (web properties) in content editor webpart - SharePoint 2010



Requirement:

Showing the property bag values of a site  in a content editor Web Part using JavaScript.


Background:

The property bag values can be called as web/site properties for a particular site.
We can see the property bag values from the SharePoint designer or you can use power shell command to see it.

In SharePoint designer , the property bag values can be seen in the site options, like the below














Solution:


Using javascript, fetching the properties and putting the scripts in a content editor webpart.

Use the below javascript code


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

 //wait until client object model dependencies are loaded before executing our code
ExecuteOrDelayUntilScriptLoaded(getWebProperties, "sp.js");

var webProperties;

function getvalue(value)
{
 return webProperties.get_item((value));

}

function getWebProperties()
 {

    var clientContext = new SP.ClientContext.get_current();

    webProperties = clientContext.get_web().get_allProperties();

    clientContext.load(webProperties);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.getWebPropertiesSucceeded),
 Function.createDelegate(this, this.onQueryFailed));
}

function getWebPropertiesSucceeded() {

var allProps = webProperties.get_fieldValues();


document.getElementById('lblName').innerHTML= webProperties.get_item('mycustomproperty');

var customProp = "";

         //make sure the property is there before using it.


if(webProperties.get_fieldValues().CustomSite_Version != undefined)

{
var customProp = webProperties.get_fieldValues().CustomSite_Version;
}

}

function onQueryFailed(args, sender)
{
     //handle errors here
}

</script>

<label style="font-weight:bold" id="lblName"></label> <br/>


Add this javascript code (including the label) in the content editor webpart's html source




Done.


You cannot invalidate the SPRequest object while it’s in use in SharePoint 2010

Cause:

If you are passing SPWeb object in the thread method, you will get this exception.

Suppose in the main thread , you have created the SPWeb object and pass this object to a thread method, like this,



 using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {

                            activateThread = new Thread(delegate()
                            {
                                this.Activate(web);
                            });
                            activateThread.Start();
                       }
                  }

Here the SPWeb object 'web' is passing to the thread method 'Activate'. The issue is because of this only.


Remedy:


Don't pass the SPWeb object from main thread (Application Thread) to a thread method. Instead create the SPWeb object inside the thread method (Here Activate method) and dispose the web object there.