Friday, August 23, 2013

Working with Code Snippet

If you are working with Visual Studio IDE for c#/vb then you came across a situation where you need to write down a same line of code at many place.

Here is the way how you can save your time from writing same line of code again and again.


(1) Write it down on "General" Tab of your Toolbox
- if you are aware of toolbox then you probably knowing tabs under this toolbox. In Toolbox, for HTML view you will find different controls available in tree view style and tabs are like "Standard", "Data", etc.. are available (below screenshot).

- You will find one more tab that is "General". In general tab you can save your frequent line of code so that whenever you require them you can easily search from this Toolbox and get those line of code you require (Keyboard shortcut for Toolbox is Ctrl + Alt + X).To do this, just copy line of code you require frequently and paste that under "General" Tab of "Toolbox" and Once you paste, it will appear under your "General" Tab (below screenshot).



You can rename its title so that it will be easily searchable code for your reference. Once you paste this line of code, you will be able to see this at both "HTML" and "Code Behind" Toolbox view and use this line of code at both places.

(2) Use Code Snippet
Visual Studio provides code snippets in built so that you can use some code samples and you are not need to write that line of code.

To use Code Snippet just write down short name used for that snippet. So, let say you are about to create new property then write "prop" and hit "Tab" key and it will automatically insert line of code for property.


You will find some links from my previous post for Code Snippet in Visual Studio.

Monday, August 5, 2013

Working with Macro Parameter In Umbraco CMS

In your Partial View, if we are passing parameters then to fetch/pass value to parameters we can use following way.

code
:var data = Model.MacroParameters;

description: This will fetch the Macro Parameter associated with our partial view. This line of code are placed in your partial view.

Now, to fetch value of your paremeter value use following line of code
string Name = data.FirstOrDefault(c => c.Key == "ParameterName").Value.ToString(); 

"ParameterName" is the name of your parameter/property which is defined in partial view.

This will fetch value passed to parameter in your partial view and you can use this value to your internal function.                  



Monday, July 29, 2013

Working with Media Items in Umbraco CMS

Different ways of  fetching media items in Umbraco CMS
Following are some different ways of fetching Media items in Umbraco CMS.

(1) To get media by id user following line of code
code : var varMedia = new umbraco.cms.businesslogic.media.Media(1);
description: This will fetch details of the media item by its id, in above code item 1 is some media item and based on this id we can find details of this media file.
-- to get file value use following line of code, in this code "umbracoFile" is the name of the property which contains name of the file
var file = varMedia.getProperty("umbracoFile");
further, you can get value by following line item
string filename = (string)file.Value


(2) To get file placed at root location on Media
code: var media = Services.MediaService.GetRootMedia().FirstOrDefault(c => c.Name == "filename");
description: This line of code will get specified file in rootmedia
-- to get file value user following line of code
string filename = media.Properties["umbracoFile"].Value.ToString()

(3) To get all files of a folder in Media
-- First, fetch id of the media folder using following line of code
var media = Services.MediaService.GetRootMedia().Where(c => c.Name == "foldername").FirstOrDefault();
- You can also check here that the item that we found is file or folder using following line of code
media.ContentType.Alias == "Folder"
-- Get if of the folder
int ID = media.Id;
-- following line of code will get all childrens/subfiles of the specified folder id. 
IEnumerable mediaChildList = Services.MediaService.GetChildren(ID);
You can loop through the child items of folder and can get specific file details

(4) To get file name using umbraco namespace on Partial View.
This line of code coulde be written on partial view.

code: var rootNode = new Node(-1).Children;
var Mainlist = ((Node)(rootNode[0])).ChildrenAsList.FirstOrDefault(c => c.Name == "foldername");

description: above line of code will get root node with given folder name
-- to get a file from above line of code
var DetailList = Mainlist.ChildrenAsList.FirstOrDefault(c => c.Name == "filename");
-- using following line of code we can get url of the file name

var src = Umbraco.Media(Convert.ToInt32(@DetailList.GetProperty("thumbnail").Value)).umbracoFile;

(5) To get file name using umbraco namespace on Partial View with other way. 
code: var rootNode = new Node(-1).ChildrenAsList.FirstOrDefault(c => c.Name == "foldername");
description: above line of code will fetch folder with name specified
-- to get child items of the folder use following line of code
var list = rootNode.ChildrenAsList;
-- to get url of the file in above folder loop through the all items in list and if file name matches, use following line of code

@node.NiceUrl

We can also get first node of the Media using following line of code code:rootNode = new Node(-1).Children;
List list = rootNode[0].ChildrenAsList;
description: above line of code will get files from first node and fetches child items from this node.       

       


WCF Application - Optimizing Performance

Wednesday, December 26, 2012

How to generate pdb file for project?

A program database (PDB) file holds debugging and project state information that allows incremental linking of a Debug configuration of your program.

If debugging is enabled but if project not generating pdb file which contains the debug configuration then it will not hit the debug point.
- To enable creating pdb file for project follow the steps given below(below screenshot Step 1).
(1) Right Click on Project and Click on Properties.
(2) Select Build Option
(3) At bottom, click on “Advanced…” button and it will open up with popup “Advanced Build Settings
(4) Under “Output” section, select “full” from dropdownlist of “Debug Info”
(5) Click on OK button
(6) Now, clean your solution and rebuild solution

Step 1

If above step not works for your or you couldn’t find location specified then follow steps given below (below screenshot Step 2).
(1) Right Click on Project and Click on Properties.
(2) Select Build Compile
(3) At bottom, click on “Advanced Compile Options…” button and it will open up with popup “Advanced Compile Settings
(4) Under “Optimizations” section, select “full” from dropdownlist of “Generate Debug Info
(5) Click on OK button
(6) Now, clean your solution and rebuild solution
 
Step 2

It will generate new pdb file for the project.
- That’s it for creating pdb file for project.

Friday, September 7, 2012

How to create folder in sharepoint custom list programmatically


Here is steps to add new item as a folder in sharepoint list programmatically.

- Create new instance of site with your current application
using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
{

- Create new web instance and open web for current application
using (SPWeb webFolder = site.OpenWeb())
{

- Get folder by folder name in list

SPFolder folder = webFolder.GetFolder("/Lists/Your List Name/Your Folder Name");

- Check whether folder is exist in list or not, if not then create new folder
if (folder.Exists)
{
Response.Write("Folder name already exists.");
return;
}

- Create list and Get County list
SPList list = webFolder.Lists[Your List Name];

- Set AllowUnsafeUpdates to true to insert new item through code
webFolder.AllowUnsafeUpdates = true;

- Create new item to add
SPListItem item = list.Items.Add();
item.FileSystemObjectType = SPFileSystemObjectType.Folder;

- Set values for the folder name
item["Title"] = “Your Folder Name”;

- fire update event for County list
item.Update();
webFolder.Update();

 }
}

-That’s it for adding new item to your sharepoint list as folder.