Pages

Showing posts with label Declarative. Show all posts
Showing posts with label Declarative. Show all posts

Thursday, December 18, 2014

Add webpart to page through module (Elements.xml) in SharePoint

Introduction

There are several ways to add pages into SharePoint page. To day i will illustrate how to add a webpart to SharePoint page through modules using Elements. xml file.

Solution

Step 1: Add a page into Visual Studio SharePoint solution. You can use this post how to do it.

You need a webpart zone to add a webpart, and according to my post the webpart zone ID is WebPartZoneSearch

Step 2: Then create a webpart as your wish.

Then the solution will be looking something like this.

image

Step 03: Open the Elements.xml file which is inside Module1 and you have to modify it something like this.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  3.   <Module Name="Module1">
  4.     <File Path="Module1\ApplicationPage1.aspx" Url="Module1/ApplicationPage1.aspx" >
  5.       <AllUsersWebPart WebPartOrder ="0" WebPartZoneID ="WebPartZoneSearch">
  6.         <![CDATA[
  7.           <webParts>
  8.             <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
  9.               <metaData>
  10.                 <type name="SitePages.WebPart1.WebPart1, $SharePoint.Project.AssemblyFullName$" />
  11.                 <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
  12.               </metaData>
  13.               <data>
  14.                 <properties>
  15.                   <property name="Title" type="string">WebPart Title</property>
  16.                   <property name="Description" type="string">WebPart Title</property>
  17.                                       </properties>
  18.               </data>
  19.             </webPart>
  20.           </webParts>
  21.         ]]>
  22.       </AllUsersWebPart>
  23.     </File>
  24.   </Module>
  25. </Elements>

Changes for your code:

Line No 05: You have to add the WebPartZoneID which is from aspx page.

Line No 10: Full Assembly name of your webpart.

Please change both according to your solution.

Conclusion

You can add other property like chrome type through Elements.xml file as well.

Please find the solution here, which may helpful to you. 

https://drive.google.com/file/d/0ByEnOE8DAdvheEJaY1RUSkdEM28/view?usp=sharing

Thursday, September 11, 2014

Client Side Rendering using JSLink – Post 02 – Customize List Forms

Introduction

In my last post I described how to startup with Client Side Rendering using JSLink with basic example. In this post I will describe “What are the places you can deploy JavaScript files in order to use in SharePoint development” and “How can we use rendering; in order to customize list forms”.

What are the places you can deploy JavaScript files in order to use in SharePoint development?

In my last post I had used layouts folder which is inside the 15 hive. But there are more options where we can deploy JavaScript files in order to use them in JSLink property. So these option can be useful for the developers who want to develop SharePoint hosted apps and who don’t have the access to local file system. JSLink property accepts below SharePoint tokens.

~site – The URL of the current website. (Recommended to use in SharePoint hosted apps development)

~sitecollection – The URL of the parent site collection of the current website.

~layouts – The URL of the Layouts virtual folder for the current website. You don’t have to worry about 14 hive or 15 hive.

~sitecollectionlayouts – The URL of Layouts folder in the current site collection (/sites/spsite/_layouts/15)

~sitelayouts – The URL of Layouts folder in the current site (/spsite/spweb/_layouts/15)

If you want to use multiple JavaScript file on a single JSLink property you can use “|” symbol.

Eg. ~layouts/folder001/test.js | ~layouts/folder002/test.js

How can we use rendering in order to customize list forms?

In the first post I described how to customize the default view form. In this post I will describe how to customize New form, Edit form and the Display form by changing the scope in JavaScript. I will show how to customize the New form and you can use the same for other forms as well.

Solution

  1. (function () {
  2.  
  3.     //   Initialize the variables for overrides objects
  4.     var overrideCtx = {};
  5.     overrideCtx.Templates = {};
  6.     overrideCtx.BaseViewID = "NewForm";
  7.     overrideCtx.ListTemplateType = "10000";
  8.  
  9.     /*
  10.    * Using the Fields override leaves the rest of the rendering intact, but
  11.    * allows control over one or more specific fields in the existing view
  12.    */
  13.     overrideCtx.Templates.Fields = {
  14.         'Title': { 'NewForm': 'Custom' }
  15.     };
  16.  
  17.     /*  
  18.      * Register the template overrides.
  19.      */
  20.     SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
  21.  
  22. })();

In the above code

BaseViewID: The form type. This can be NewForm, EditForm, DisplayForm.

ListTemplateType: This is List Template type. You can see your list template type in Elements.xml file belonging to the list definition in Visual Studio.

image image

The above JavaScript code shows how to modify the title value in the form.

Now you can save the JavaScript file in any locations as said under first heading.

We have one more thing to do. Reference the JavaScript file with the list for new form. 

Method 01 – Declaratively

I will show how to define JSLink in SPList Schema.xml file.

  1. Open the schema of List definition in your visual studio solution
  2. There at the bottom you can find the types of form.

image

3. Add a new property as JSLink and  give the location as mentioned below.

image

Now deploy the solution.

Method 02 – Programmatically in C#

Now I will show how to add JSLink property to NewForm programmatically through C#.

  1. SPWeb spWeb = SPContext.Current.Web;
  2.             SPList spList = spWeb.Lists.TryGetList("List Name");
  3.             SPForm newForm = spList.Forms[PAGETYPE.PAGE_NEWFORM];
  4.             SPFile page = spWeb.GetFile(newForm.Url);
  5.             using (SPLimitedWebPartManager wpManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
  6.             {
  7.                 // Enable Update
  8.                 wpManager.Web.AllowUnsafeUpdates = true;
  9.  
  10.                 //Check out the file
  11.                 SPFile file = wpManager.Web.GetFile(newForm.Url);
  12.                 if (file.CheckOutType == SPFile.SPCheckOutType.None)
  13.                     file.CheckOut();
  14.  
  15.  
  16.                 foreach (WebPart webpart in wpManager.WebParts)
  17.                 {
  18.                     if (webpart is ListFormWebPart)
  19.                     {
  20.                         var listFormWebPart = (ListFormWebPart)webpart.WebBrowsableObject;
  21.                         listFormWebPart.TemplateName = "ListForm";
  22.                         listFormWebPart.JSLink = "/_layouts/15/CSR002/test.js";
  23.                         wpManager.SaveChanges(listFormWebPart);
  24.                     }
  25.                 }
  26.  
  27.                 // Update  file
  28.                 file.Update();
  29.                 file.CheckIn("Added JSLink");
  30.                 wpManager.Web.AllowUnsafeUpdates = false;
  31.             }

Actually what we are doing is – we are setting the value to JSLink property in web part on NewForm.aspx page.

The code below will NOT WORK.

  1. SPWeb spWeb = SPContext.Current.Web;
  2.             SPList spList = spWeb.Lists.TryGetList("CustomList002");
  3.             SPForm newForm = spList.Forms[PAGETYPE.PAGE_NEWFORM];
  4.             if (null != newForm)
  5.             {
  6.                 newForm.JSLink = "/_layouts/15/CSR002/test.js";
  7.             }
  8.             spWeb.AllowUnsafeUpdates = true;
  9.             spList.Update();
  10.             spWeb.AllowUnsafeUpdates = false;

Method 03 – Browser user interface

Go to the NewForm.aspx page where we try to add new item by clicking add new item button. Please refer my first post where I have described how to set JSLink property.

Conclusion

Once we deploy and give reference to our JavaScript file we can see that our Title value is changed to the customized value.

image

Keep in touch. I will cover more advanced topics on this.

Wednesday, September 3, 2014

Create Discussion Board list definition through Visual Studio 2012 for SharePoint 2013

Introduction

There was a requirement to create a discussion board list definition through visual studio. So that our client can be able to create list instance with some customization.

Problem Background

We tried to create content type by inheriting Discussion and then created a custom list definition using created content type. But it was not same as the out of the box discussion board – We couldn’t get the reply page and the views as in the ootb.

Solution

  • Create a new SharePoint solution
  • Add a content type inheriting from Discussion
    • Right Click on the project –> Add –> New Item –> Content Type –> Name it (I have named it as DisBoardCT) –> Click Add –> Select Discussion from Content type setting page

image

  • Create a List
    • Right Click on the project –> Add –> New Item –> List –> Click Add select the first option from Choose List Setting Page

image

  • Add the above created content type to our list

image

image

Now you list definition is ready. But this will not work as the out of the box discussion board.

  • Now you have to change the schema.xml
    • Go to the OOTB list definition usually the location is C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\FEATURES\DiscussionsList\Discuss
    • Copy the content in schema.xml and paste on our schema.xml in the visual studio
    • Get the Content type ID which we created.

    image

    • Replace the content type ID in schema.xml with our ID. There will be couple instances of the default content type. “0x012002”image
    • Once you change the content type in schema.xml the default content type in will be set to our custom content type. Please verify

image

  • Change the Type
    • Change the Type to 108 and the Image to

      Image="/_layouts/15/images/itdisc.png?rev=23"/>

      image

  • Change the Template type to 108 in list instance elements file.

image

image

  • Now deploy the solution and you should be able to get see the our custom discussion board list definition.

Conclusion

I have created a sample and you may download it here.

https://drive.google.com/file/d/0ByEnOE8DAdvhWUJ4ektWR2RBQXc/view?usp=sharing

Monday, August 18, 2014

Add items to SharePoint list declaratively through Visual Studio

Problem

We had to add couple items to SharePoint list declaratively. So that when we deploy the solution, SharePoint list and the default items will be provisioned in SharePoint site.

Solution

Step 01:

Add a SharePoint list to Visual Studio.

Step 02:

Open the Elements.xml file of list instance.

image

So you will see something like this.

image

Step 03:

Add the data.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  3.   <ListInstance Title="List1"
  4.                 OnQuickLaunch="TRUE"
  5.                 TemplateType="10000"
  6.                 Url="Lists/List1"
  7.                 Description="My List Instance">
  8.     <Data>
  9.       <Rows>
  10.         <Row>
  11.           <Field Name="Title">Item 001</Field>
  12.         </Row>
  13.         <Row>
  14.           <Field Name="Title">Item 002</Field>
  15.         </Row>
  16.       </Rows>
  17.     </Data>
  18.   </ListInstance>
  19. </Elements>

Here the Name in the field will be column name.

Conclusion

Right click and deploy. Then open the list in the browser and you can see the items.

image You can download the basic sample solution from the below link.

https://drive.google.com/file/d/0ByEnOE8DAdvhVjB4RVkxUk9CcWM/edit?usp=sharing