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.

Saturday, June 30, 2012

Delete sharepoint list item attachment programmatically




Here is steps to delete sharepoint list item attachment programmatically.

- Follow the steps to delete list item attachment from your sharepoint list.

- 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 web = site.OpenWeb())
{

- Create new list object and get your list
SPList SPLData = web.Lists["Your List Name"];

- Create new query object to hold query which fetches list item of the list
SPQuery SPMyQuery = new SPQuery();

- Assign query with value to it in ID field of the list item

SPMyQuery.Query = string.Format(“<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>{0}</Value></Eq></Where>”,1);

//========== Fetch item collection data for Your List  ==========//
SPListItemCollection common = SPLData.GetItems(SPMyQuery);

SPListItem listItem = common[0];

if (listItem.Attachments.Count > 0)
{
listItem.Attachments.Delete(“Name of file”);
listItem.Update();
}

}

}

-That’s it for deleting attachments from your existing list item of sharepoint list.

Wednesday, June 13, 2012

Get sharepoint list item attachment programmatically


Here is steps to get sharepoint list item attachment programmatically.

- Follow the steps to insert new item into your sharepoint list.

- 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 web = site.OpenWeb())
{

- Create new list object and get your list
SPList SPLData = web.Lists["Your List Name"];

- Create new query object to hold query which fetches list item of the list
SPQuery SPMyQuery = new SPQuery();

- Assign query with value to it in ID field of the list item
SPMyQuery.Query = string.Format(“<Where
>  < Eq >  < FieldRef Name='ID' / >  < Value Type='Counter' > {0} < /Value >  < /Eq >  < /Where >”,1);

- Assign query to list and get item on the basis of the query build above
SPListItemCollection common = SPLData.GetItems(SPMyQuery);

- Check whether list collection contains result of the query, if contains item the proceed to update value of the list item
if (common.Count > 0)
{

SPListItem mylistItem = common[0];
string[] _Str_Attachments = GetAttachmentCollection(mylistItem);

- You can assign attachment collection with grid and if you want to give link to the attachment filename, on each file name with anchor link put following code for href of anchor.
string.Format("/Lists/{0}/Attachments/{1}/{2}", "Your List Name”,1,”FileName”);

}
}
}

- Get sharepoint list item attachment collection
public string[] GetAttachmentCollection(SPListItem item)
{
string FileNames = String.Empty;
if (item.Attachments.Count > 0)
{
string[] _Str_File_Name = new string[item.Attachments.Count];
for (int i = 0; i < item.Attachments.Count; i++)
{
SPFile file = item.ParentList.ParentWeb.GetFile(item.Attachments.UrlPrefix + item.Attachments[i].ToString());
_Str_File_Name[i]= file.Name;
}
return _Str_File_Name;
}
return null;
}



-That’s it for fetching attachments for existing item of sharepoint list.