another technical blog...technically

Wednesday, April 10, 2013

About SharePoint:FormField and custom column

Yesterday, i had to face SharePoint again.
I created a custom type note field in which i inject custom HTML code (generated by code behind):

 
 

and a custom edit form (for the related custom content type) in which i want to show this field.
 

When i run the form, i noticed the field was blank...
What i've done to solve the problem?
This was driving me mad, i solved just setting the No value to the Append Changed to Existing Text option of the custom field.
The wanted
VoilĂ , smooth and clean!

Tuesday, April 2, 2013

Workflow task SPUser field

Working with workflow, i had this problem: i created a User type custom field to map who is the user who started the workflow.
I know there is a builtin field which does this work, but i need to create a custom one. So i tried to set the value with this code:
this.taskProperties.ExtendedProperties["RequestedBy"] = this.WorkflowProperties.OriginatorUser;
The result was an exception. I tried to create an SPUserFieldValue in order to set the extended property using this object. Since i receive another exception i looked into the ULS logger and i found this:

System.InvalidOperationException: The platform does not know how to deserialize an object of type Microsoft.SharePoint.SPFieldUserValue. The platform can deserialize primitive types such as strings, integers, and GUIDs; other SPPersistedObjects or SPAutoserializingObjects; or collections of any of the above. Consider redesigning your objects to store values in one of these supported formats, or contact your software vendor for support.
The code below solved the problem
this.taskProperties.ExtendedProperties["RequestedBy"] = this.WorkflowProperties.Originator;
Morality is: don't try stupid things and also refer to primitive types.
Share:

Wednesday, March 20, 2013

Embarrassing stories: Visual Studio toolbox shows only General and HTML Tabs

My boss said "Varro, don't dare to use layouts folder or the custom form will be available to the farm, that's not good", so yesterday i tried to create an application page and deploy it using a module. So, when you create an application page in a SharePoint project, Visual Studio automatically  creates the layouts mapped folder and puts the .aspx file there.
After that you can easily drag and drop the aspx file in the module and continue to work the file under the module itself... theorically.
When i dragged the page into the module, only HTML and General tabs were available on the ToolBox panel, and the ASP and SharePoint elements inserted in the page were marked as unexisting in the code behind.
The problem
I started looking around for a possible solution on the web (moreover i was working on a customer VM used by other people, so don't know what happened on this VM) and tried almost everything.
After a hour i clicked on View Markup and... magically the problem solved. I tested the whole process on a new solution on my local machine and i haven't met the same problem.
The solution
The proof
Morality is: if you have this issue, before risking to waste time trying everything, try this "trick".

Tuesday, March 5, 2013

Get site content types and site columns using PowerShell

Lately i'm working to a feasibility study for a intranet migration from SharePoint 2010 to SharePoint 2013.
During this project, i needed to know what are content types and fields were involved in the 2010 custom solution, in order to map them in SharePoint 2013 ones.
My boss asked me to do this A.S.A.P., so i developed a powershell script capable of retrieving these data and put them in CSV files, so i can import them with excel and do further analysis.
The script output is a set of three CSV files:
  1. Site content types
  2. Site columns
  3. Mapping between content types and fields
Here is the code for PowersShell 3.0:
##
## 20130214, from Varro with love
##

Add-PSSnapin Microsoft.SharePoint.PowerShell

## Variables
$WEB_APPLICATION_URL = "http://teamsite"
$CTS_FILE_PATH = "c:\Users\Administrator\Desktop\2013_TeamSite_ContentTypes.csv"
$FIELDS_FILE_PATH = "c:\Users\Administrator\Desktop\2013_TeamSite_Fields.csv"
$CT_FIELDS_FILE_PATH = "c:\Users\Administrator\Desktop\2013_TeamSite_CTs_And_Fields.csv"


## Initialization
echo "Initializing variables"
'"CT Name","CT Description","CT ID","CT Group","CT Parent ID"' | Out-File $CTS_FILE_PATH;
'"Field Title","Field Description","Field Internal Name","Field ID","Field Group","Field Type"' | Out-File $FIELDS_FILE_PATH;
'"CT Name","Field Title","CT ID","Field ID"' | Out-File $CT_FIELDS_FILE_PATH;

$AllContentTypes = @();
$AllFields = @();


## Go with the code
echo "Entering web application $WEB_APPLICATION_URL" 

$webApplication = Get-SPWebApplication -identity $WEB_APPLICATION_URL

ForEach ($siteCollection in $webApplication.Sites)
{
    ForEach($subSite in $siteCollection.AllWebs)
    {
        $subsiteUrl = $subsite.Url;

        echo "Extracting info from $subSiteUrl";

        $contentTypes = $subSite.ContentTypes;

        ForEach ($contentType in $contentTypes)
        {
            If (!$AllContentTypes.Contains($contentType.Id))
            {
                $AllContentTypes += $contentType.Id;

                # Let's write on Content types CSV
                '"' + $contentType.Name + '","' + $contentType.Description + '","' + $contentType.Id + '","' + $contentType.Group + '","' + $contentType.Parent.Id + '"' | Out-File $CTS_FILE_PATH -Append
           
                ForEach ($field in $contentType.Fields)
                {
                    If (!$AllFields.Contains($field.Id))
                    {
                        $AllFields += $field.Id;

                         # Let's write on Fields CSV
                        '"' + $field.Title + '","' + $field.Description + '","' + $field.Id + '","' + $field.Group + '","' + $field.Type + '","' | Out-File $FIELDS_FILE_PATH -Append;
                    }

                    '"' + $contentType.Name + '","' + $field.Title + '","' + $contentType.Id + '","' + $field.Id + '","' | Out-File $CT_FIELDS_FILE_PATH -Append;
                }
            }
        }
    }
}

echo "See you space cowboy";
If you need to run this script in PowerShell 2.0, you just need to change these rows
If (!$AllContentTypes.Contains($contentType.Id))
...
   If (!$AllFields.Contains($field.Id))
with these ones:
$alreadyInCT = $AllContentTypes -contains $contentType.Id;
If (!$alreadyInCT)
...         
   $alreadyInFields = $AllFields -contains $field.Id;               
   If (!$alreadyInFields)
Please feel free to reuse, comment and share.
Share:

Me, myself and I

My Photo
I'm just another IT guy sharing his knowledge with all of you out there.
Wanna know more?