STEP 1 :Copy the PowerShell scriptlet below and paste it in a notepad, modify your Site Collection URL in the code marked in red below and save it with a .ps1 extension.
=======================================================================
Add-PsSnapin Microsoft.SharePoint.PowerShell
#Site Collection URL - Give your site collection url in quotation marks
$SiteUrl="http://localhost"
#Do not modify anything in the script from here onwards
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
#solutions to deploy
$SolutionName="PrintListItem.wsp"
$SolutionPath = Join-Path (Get-ScriptDirectory) $SolutionName
echo "Extracting information from $SolutionPath"
$SolutionPath= $SolutionPath
$FeatureName="PrintListItem_Feature1"
#Feature name
$FeatureID= $(Get-SPFeature -limit all | ? {($_.displayname -eq $FeatureName)}).Id
$SingleSiteCollection = Get-SPSite $SiteUrl
#Admin service
$AdminServiceName = "SPAdminV4"
$IsAdminServiceWasRunning = $true;
if ($(Get-Service $AdminServiceName).Status -eq "Stopped")
{
$IsAdminServiceWasRunning = $false;
Start-Service $AdminServiceName
Write-Host 'SERVICE WAS STOPPED, SO IT IS NOW STARTED'
}
Write-Host 'DEACTIVATING FEATURE ...'
Disable-SPFeature -Identity $FeatureName -Url $SiteUrl -Confirm:$false
Write-Host 'FEATURE HAS BEEN DEACTIVATED SUCCESSFULLY.'
#Uninstall
Write-Host 'UNINSTALLING SOLUTION ...'
$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $true)}
if ($Solution -ne $null)
{
if($Solution.ContainsWebApplicationResource)
{
Uninstall-SPSolution $SolutionName -AllWebApplications -Confirm:$false
}
else
{
Uninstall-SPSolution $SolutionName -Confirm:$false
}
}
while ($Solution.JobExists)
{
Start-Sleep 2
}
Write-Host 'SOLUTION HAS BEEN UNINSTALLED SUCCESSFULLY.'
Write-Host 'REMOVING SOLUTION ...'
if ($(Get-SPSolution | ? {$_.Name -eq $SolutionName}).Deployed -eq $false)
{
Remove-SPSolution $SolutionName -Confirm:$false
Write-Host 'SOLUTION HAS BEEN REMOVED SUCCESSFULLY.'
}
Write-Host 'ADDING SOLUTION ...'
Add-SPSolution $SolutionPath | Out-Null
Write-Host 'SOLUTION HAS BEEN ADDED SUCCESSFULLY.'
Write-Host 'DEPLOYING SOLUTION ...'
$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $false)}
#use '-force' paramater to install all commands in this if statement
if(($Solution -ne $null) -and ($Solution.ContainsWebApplicationResource))
{
Install-SPSolution $SolutionName –AllwebApplications -GACDeployment -Force -Confirm:$false
}
else
{
Install-SPSolution $SolutionName -GACDeployment -Force -Confirm:$false
}
while ($Solution.Deployed -eq $false)
{
Start-Sleep 2
}
Write-Host 'SOLUTION HAS BEEN DEPLOYED SUCCESSFULLY.'
Write-Host 'ACTIVATING FEATURE ...'
if ($FeatureName -ne $null)
{
Enable-SPFeature -Identity $FeatureName -Url $SiteUrl -Confirm:$false
}
#message
Write-Host 'FEATURE HAS BEEN ACTIVATED SUCCESSFULLY.'
if (-not $IsAdminServiceWasRunning)
{
Stop-Service $AdminServiceName
}
Remove-PsSnapin Microsoft.SharePoint.PowerShell
Echo Finish
Step 2 :
Now create a new notepad file and copy the code mentioned below, and then save the below mentioned code in a .bat file to automate the PowerShell deployment.
cd /d %~dp0
powershell -noexit -file ".PrintListItemDeploymentScript.ps1" "%CD%"
pause
Showing posts with label SharePoint 2010. Show all posts
Showing posts with label SharePoint 2010. Show all posts
Monday, June 20, 2011
Tuesday, March 29, 2011
Monday, March 28, 2011
Create a Custom Field, Content Type, List Definition, and List Instance
- Visual Studio 2010.
First, create a custom field item as a SharePoint project in Visual Studio called Patient Name.
To create a custom field item
- Display the New Project dialog box by pointing to New on the File menu, and then clicking New Project.
- Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.
- Click Empty SharePoint Project in the Templates pane, change the name of the project to CustomField1, and then click OK.
The SharePoint Customization Wizard appears. The Empty Project template is used for custom field items because there is no template for them. - On the Specify the site and security level for debugging page, enter the URL for the SharePoint server site to which you want to add the new custom field item, or use the default location (http://<system name>/).
- In the What is the trust level for this SharePoint solution? section, use the default value Deploy as a sandboxed solution.
For more information about sandboxed and farm solutions, see Sandboxed Solution Considerations. - Click Finish. You should see the project appear in Solution Explorer.
- Add an Empty Element project item to the project. To do this, click the field's project node (CustomField1) in Solution Explorer and then click Add New Item on the Project menu.
- Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.
- In the Templates pane, select Empty Element, keep the default name EmptyElement1, and then click Add.
- In the Elements.xml file under EmptyElement1, add the following after the <Elements> tag:
This adds a field that is named Patient Name.
<Field ID="{5744d18c-305e-4632-8bd1-09d134f4830d}" Type="Note" Name="PatientName" DisplayName="Patient Name" Group="A Custom Group"> </Field>
Important
When adding a custom field, you must specify the ID, Type, Name, DisplayName, and Group attributes for the field to appear correctly in SharePoint. Statement completion erroneously indicates that you need only the first three of these attributes when in fact all five are required. - Press F5 to run and deploy the project to SharePoint.
If you get a message that script debugging is disabled, click Yes to continue debugging. - If this is the first time you have debugged a Web application in Visual Studio, you will be asked whether you want to modify the Web configuration file to enable debugging. Answer Yes.
- At the top of the SharePoint Web page, click the Site Actions button and then click Site Settings.
- Under the Galleries section of the Site Settings page, click the Site columns link.
- In the Site Column Gallery page, scroll the page down to the Custom Columns heading and note the new field item added, Patient Name.
- Close the browser.
Next, create a content type, based on the SharePoint Contacts list, that uses the new field created in the previous procedure.
To create a custom content type
- Add a content type to the project. To do this, click the project node in Solution Explorer and then click Add New Item on the Project menu.
- Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.
- In the Templates pane, select Content Type and use the default name, ContentType1.
- In the SharePoint Customization Wizard, select Contact as the content type on which to base the new one, and then click Finish.
This opens the content type's Elements.xml file for editing. - In the <FieldRefs> section of Elements.xml, copy and paste in:
The FieldRef ID used here refers to the Field ID we created for the custom field. - Press F5 to run and deploy the project.
- At the top of the SharePoint Web page, click the Site Actions button and then click Site Settings.
- Under the Galleries section of the Site Settings page, click the Site content types link.
- In the Site Content Types Gallery page, note the new content type we just created, CustomField1 - ContentType1. Click it to view its fields. In the list of fields for CustomField1 - ContentType1, note the field Patient Name is added.
- Close the browser.
Now, create a list definition and list instance that will use the new content type and field. Because we want to create the list definition based on the content type we just created, we will use the List Definition From Content Type project item template.
To create a list definition and list instance
- Add a list definition to the project. To do this, click the project node in Solution Explorer and then click Add New Item on the Project menu.
- Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.
- In the Templates pane, select List Definition From Content Type and use the default name, ListDefinition1.
- In the What is the display name of the list definition? box, leave the default value, CustomField1 - ListDefinition1.
- In the What content type would you like to use for your list definition? list, use the default (and only) value, CustomField1 - ContentType1.
This creates a customizable list definition based on the new, modified Contacts list. - Select the Add a list instance for this list definition box to add a list instance project item to the project. The list instance will be an instance of the new list definition.
- Click Finish. You should see the list definition appear in Solution Explorer.
Finally, customize the list definition by adding a new field to it, Patient Name.
The value of the Name attribute in the list definition's Elements.xml must match the folder name of the list definition project item or an error will occur when the project is run. To make sure that the Name attribute matches the folder name, the replacement parameter (or token) $ProjectItemName$ is used. When the SharePoint project is packaged and deployed, this token is converted into the actual folder name of the list definition. If you change the value of theName attribute to something other than this token, then your application may not run correctly. For more information, see Replaceable Parameters. |
To add a field to the list definition
- In Solution Explorer, double-click Schema.xml under ListDefinition1 to view it.
Schema.xml defines all of the fields and other elements of the list definition, such as name and address. Notice under the <Fields> element at the top of the file that the following field definition was added by the content type:
- Press F5 to run and deploy the project.
- Under the Lists section in the SharePoint QuickLaunch bar, click the CustomField1 - ListInstance1 link.
This is the list instance for the new list definition. Note that the new Patient Name field does not yet appear as a column in the list instance. - Click List in the List Tools menu tab at the top of the page and then click the Modify View button on the ribbon bar.
- In the list of available Column Names, select Patient Name and then click OK. Notice that the Patient Name field now appears in the list instance.
Create Custom List Definitions in SharePoint 2010
Create a SharePoint List Definition Project
In this task, you create a list definition SharePoint 2010 project in Microsoft Visual Studio 2010.
To create the SharePoint project
- To start Visual Studio 2010, click the Start Menu, click All Programs, click Microsoft Visual Studio 2010, and then click Microsoft Visual Studio 2010.
- On the File menu, point to New, and then click Project.
- In the New Project dialog window, in the Installed Templates section, click Visual C#, click SharePoint, and then click 2010.
- Click List Definition from the project items.
- In the Name box, type Bugs and then click OK.
- In the SharePoint Customization Wizard, type the local Web site that you want to use for this exercise (such as http://localhost/SampleWebSite).
- For the trust level, select Deploy as a farm solution and then click Next.
- In the What is the display name of the list definition? Box, type Bugs.
- In the What is the type of the list definition? drop-down list, select Custom List.
- Select the Add a list instance for this list definition check box and then click Finish.
Customize the SharePoint List Definition
This task guides you through customizing the List Definition project template by changing the list name, the content type it stores, and the fields that are displayed in the new form, edit form, and display form.
To customize the list definition
- In Solution Explorer, expand ListInstance1 and open the Elements.xml file.
- Within the ListInstance element change the Title attribute to Bugs and change the TemplateType attribute to 10001.
- In Solution Explorer, before Schema.xml, open Elements.xml.
- Within the ListTemplate element, change the Type attribute to 10001 and add the
DisallowContentTypes=”FALSE”attribute. - Insert the following XML into the top of the Elements element in the Elements.xml file as shown in Figure 1. This XML describes the Bug Item content type the list stores.
<!-- Describes content types. --> <ContentType ID="0x010089E3E6DB8C9B4B3FBB980447E313CE94" Name="Bug Item" Group="Custom Content Types" Description="Bug item content type." Version="0"> <FieldRefs> <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" /> <FieldRef ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" /> <FieldRef ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" /> <FieldRef ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" /> </FieldRefs> </ContentType> <!-- End describes content types. -->
- Insert the following XML into the top of the Elements element in the Elements.xml file as shown in Figure 1. This XML describes the fields the Bug Itemcontent type uses.
<!-- Describes fields in bug list. --> <Field Type="Note" DisplayName="Description" Required="FALSE" NumLines="6" RichText="FALSE" Sortable="FALSE" ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" StaticName="BugDescription" Name="BugDescription" Group="Custom Columns" /> <Field Type="Text" DisplayName="Project" Required="FALSE" MaxLength="255" ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" StaticName="BugProject" Name="BugProject" Group="Custom Columns" /> <Field Type="Text" DisplayName="Assigned To" Required="FALSE" MaxLength="255" ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" StaticName="BugAssignedTo" Name="BugAssignedTo" Group="Custom Columns" /> <!-- End describes fields in bug list. -->
Figure 1. Elements.xml file after inserting two code sections
- In Solution Explorer, open Schema.xml.
- Add the
EnableContentTypes=”TRUE”attribute to the List element inside the Schema.xml file. - Insert the following XML into the ContentTypes element in the schema.xml file as shown in Figure 2. This XML describes the Bug Item content type this list stores.
- Insert the following XML into the Fields element as shown in Figure 2. This XML describes the fields the list stores. These are directly related to the fields in the content type that you added in the previous step.
<!-- Start Add. --> <Field Type="Note" DisplayName="Description" Required="FALSE" NumLines="6" RichText="FALSE" Sortable="FALSE" ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" StaticName="BugDescription" Name="BugDescription" Group="Custom Columns" /> <Field Type="Text" DisplayName="Project" Required="FALSE" MaxLength="255" ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" StaticName="BugProject" Name="BugProject" Group="Custom Columns" /> <Field Type="Text" DisplayName="Assigned To" Required="FALSE" MaxLength="255" ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" StaticName="BugAssignedTo" Name="BugAssignedTo" -Group="Custom Columns" /> <!-- End Add. -->
Figure 2. Schema.xml after inserting the ContentTypeRefs code and Fields code
- Insert the following XML into the ViewFields element in the second view, where
BaseViewID="1"as shown in Figure 3. This XML describes which fields should be visible in this particular view. BaseView 1 is set as the default view. This is the view the newly created fields should display in.
<!-- Start Add. --> <FieldRef Name="BugDescription"></FieldRef> <FieldRef Name="BugProject"></FieldRef> <FieldRef Name="BugAssignedTo"></FieldRef> <!-- End Add. -->
Figure 3. Schema.xml after Fields XML insert
- In Solution Explorer, right-click the Bugs node, and then click Deploy.
Test the Solution
In this task, you test the solution by adding a bug to the Bugs list.
To add an item to the Bugs list
- Open the website that you specified previously.
- On the Home page, click the Bugs list in the left navigation pane.
- On the List Tools tab, click Items, and then click Bug Item on the New Item drop-down list as shown in Figure 4.
Figure 4. Bug Item from the New Item drop-down list
The Bugs - New Item screen is displayed as shown in Figure 5.
Figure 5. New Bug Item dialog box
- In the Bugs - New Item screen, type Bug01 in the Title box.
- Type Who is going to track this bug? in the Description box, type Writing developer articles in the Project box, and then click Save.
Create a Custom List Definition
A field definition defines the columns that appear inside of the list template. For more information about the XML that is used to define a field type.
To create the field definition
- Start Microsoft Visual Studio 2010.
- On the File menu, point to New, and then click Project.
- In Project Types, under Visual Basic or C#, select Empty SharePoint Project.
- Type FavoriteColorList as the project name. Click OK.
- In the SharePoint Customization Wizard, choose Deploy as a sandboxed solution. Click Finish.
- In the Solution Explorer, right-click the FavoriteColorList project and select Add and then New Item.
- In the Add New Item dialog box, choose the Empty Element template and type FavoriteColorField as the Name. Click Add.
- Insert the following XML inside the Elements element. This XML defines a field for the content type.
Important
You must replace FieldId in the ID attribute with a GUID. To generate a GUID, click Tools and then click Create GUID.
A content type defines a collection of items to use inside of the list. For more information about content types, see Content Types.
To create the content type
- In the Solution Explorer, right-click the FavoriteColorList project and select Add and then New Item.
- In the Add New Item dialog box, choose the Content Type template and type FavoriteColorContentType as the Name. Click Add.
- In the Choose Content Type Settings dialog box, choose Item in the Which base content type should this content type inherit from? drop-down list box. Click Finish.
- In the FieldRefs element, you must add a FieldRef element that references the field that you previously defined. The following XML adds a reference to the field.
Important
The ID attribute of the FieldRef element must match the ID attribute of the Field element.
To create the list definition from the content type
- In the Solution Explorer, right-click the FavoriteColorList project and select Add and then New Item.
- In the Add New Item dialog box, choose the List Definition From Content Type template and type FavoriteColorListDefinition as the Name. Click Add.
- In the Choose List Definition Settings dialog box, ensure that FavoriteColorContentType is selected in the What content type would you like to use for your list definition? drop-down list box.
- Check the Add a list instance for this list definition option. Click Finish.
- Next, open the Schema.xml file that was created.
- In the ViewFields element, add the same FieldRef element that you added to the content type. The following XML adds a reference to the field.
Important
The ID attribute of the FieldRef element must match the ID attribute of the Field element.
Subscribe to:
Posts (Atom)