Tuesday, May 22, 2012

How to know existing SharePoint site template name and ID

Scenario :

I want to know what is the template name used  while creating site for each site under a site collection and i want to store each site template name in a list like "sitename -- templatename"

Steps Involved

TIP:  We can find site template name for a particular site 
step 1: open ur site in browser and right click select view source. in  view source find the following line of text "var g_wsaSiteTemplateId="

example: "var g_wsaSiteTemplateId = 'STS#0';" --- STS is the template name and 0 is the template id

2. var g_wsaSiteTemplateId = 'SDPROJ1#0'; ---- SDPROJ1 is the custom template name and 0 is the id.

first i tried to get template name for a site using client object model and display in a message box but unfortunately using client object model we cant get template name for a site.

Then i tried with server object model , Here the steps..

 here am creating one webpart in that am taking one button and one label.

in button click event am writing my code for getting template name for each site and saving it into a list called "AllSitesTemplates"

Here the code :

 string str = "";
                SPWebApplication myWebApp = SPContext.Current.Site.WebApplication;
                SPSiteCollection mySiteCollection = myWebApp.Sites;
                SPSite oSiteCollection = SPContext.Current.Site;
                SPWeb oWebsiteRoot = oSiteCollection.OpenWeb();
                SPList oList = oWebsiteRoot.Lists["AllSitesTemplates"];
                foreach (SPSite mySite in mySiteCollection)
                    SPWebCollection myWebCollection = mySite.AllWebs;
                    foreach (SPWeb web in myWebCollection)
                        str += "Web Name : " + web.Title;
                        str += "\n Web Template Name : " + web.WebTemplate;
                        str += "\n Web Template ID : " + web.WebTemplateId;
                        str += "\n Web GUID : " + web.ID+" ";
                        SPListItemCollection listItemCOll;

                        //add the item in the User List
                        SPListItem oListItem = oList.Items.Add();
                        oListItem["Title"] = web.Title;
                        oListItem["TemplateName"] = web.WebTemplate + "#" + web.WebTemplateId;

            catch (Exception ex)
                str+="Error: " + ex.Message;
            Label1.Text = str;

Now deploy your webpart and add this web part to page and click the button.

Now click the button and go to AllSitesTemplates list and u can find the result.

Friday, May 18, 2012

How to know internal field name of a list column in SharePoint 2010

Here am giving one of the solution for how to know the internal field name of a column in a list

Involved Steps

Step 1. Go to your list in browser.
Step 2: Go to library setting and click on list column name (to which column u want to know internal name).
now check address bar u can find "&Field=xxxxx" here xxxxx is the internal name of the field.

Thursday, May 17, 2012

How can we restrict specific file type uploading on SharePoint Library?

?If a user want to restrict a file type uploading on sharepoint library like .zip

Steps Involved in solution:

1. Go to central administration and select security from left side panel
2. Now select Define blocked file types under General Security section in Security.

3. Now select web application(right top side) and change web application to your web application.

4. Now enter your file type which you want to restrict i.e zip and click on "OK"

5. Now try to upload a zip file it will throw an error while uploading zip file.

6. If you want to unblock zip file repeat step 1,2,3
7. Now remove your file type from list of file types and click on "OK".

 now try to upload a zip file this time it wont throw any error.


What is Out Of Box ( OOB ) in SharePoint 2010

In SharePoint, many users use the term OOB, what is OOB?

It is OOB when there is no need to deploy server code.
It is OOB when the user does not need to do any coding all.

OOB just means it comes with the program and can be deployed without needing to buy anyting else or do any custom programming to make it work. Spellcheck is OOB with Word. Document Library's are OOB with MOSS. The ability to search is OOB with MOSS.

Users may use a CEWP or a CQWP and enter JScript and CAML code. This requires user coding, but no server side deployment.

Typically the term Out of Box is used by any product company (the product here is SharePoint, Company is Microsoft) to convey that the product provides a feature and there is minimal customization needed for making that product feature meet the needs of your business.

Here are pros for going OOB:

No need to write code
No need to debug code
No need to test code
No need to optimize code, such as cache, log, exception handling, etc.
Easier to understand (not always)
Easier to deploy (not always)
Shorter development cycle
Save time and $

And of course there are cons:

Harder to debug and test
Limited options for optimization
May not be feasible
May be harder to deploy

Wednesday, May 16, 2012

How to Add/Create Custom Properties Visual Web part in SharePoint

Visual web part By default provide some properties like layout properties. Like this we can also add properties to Visual web part.
i have one label control in my webpart i want to set fontsize and forntcolor for that label

Involved Steps

1. Open Visual Studio 2010 and Select new project
2. Select visual webpart template and Give some name to project( here i given CP4VWP)

3. Double Click on visualwebpart1 which is after package in solution explorer.
4. Now add controls to visual webpart. here am adding one button and one label.

5. Now expand Visual web part1 and open VisualWebPart1.cs then add the following code

    public class VisualWebPart1 : WebPart
        public static string _fontSize;
        System.Web.UI.WebControls.WebParts.WebDisplayName("Enter the font size Value"),
        System.ComponentModel.Category("Uday Custom Properties"), System.ComponentModel.DefaultValue("")]
        public string _FontSize
            get { return _fontSize; }
            set { _fontSize = value; }
        public static string _fontColor;
         System.Web.UI.WebControls.WebParts.WebDisplayName("Enter the font color"),
         System.ComponentModel.Category("Uday Custom Properties"), System.ComponentModel.DefaultValue("")]

        public string _FontColor
            get { return _fontColor; }
            set { _fontColor = value; }
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.

6. now Go to webpart page(VisualWebPart1UserControl.ascx) and double click on button and write the following code in VisualWebPart1UserControl.ascx.cs file button click event

if (VisualWebPart1._fontColor == null || VisualWebPart1._fontSize == null || VisualWebPart1._fontColor == "" || VisualWebPart1._fontSize == "")
                Label1.Font.Size = Convert.ToInt32(VisualWebPart1._fontSize);
                Label1.ForeColor =System.Drawing.Color.FromName(VisualWebPart1._fontColor);

we are adding fontcolor to label so we need to add the reference "System.Drawing" and add the following namespace "using System.Drawing;"

7. Now deploy webpart and add the webpart to your webpart page.

8. Now select web part and click edit web part, you can find a category name with "Uday Custom Properties" at bottom. Give font size and font color name and click on OK button
9. Now Click on Set button on webpart. label fontsize and fontcolor should be change.


Debugging SharePoint 2010 Web Part from Visual Studio 2010

Involved Steps

1. Deploy your webpart
2. Add the webpart to webpart page in a sharepoint site.
3. Place a break point in that webpart code file.

4.GO To "Debug" menu, and choose "Attach to Process".

5.By default the following settings are selected in "Attach to:"

under "Attach to: " section, Click on  "Select Button"

By default "Automatically determine the type of code to debug" is selected. Now change the selection to

"Debug these code types" and check "Managed(V2.0,V1.1,V1.0)" and click "OK".

6. Check the "Show processes in all sessions" check box.

7. Now scroll down in Available process and select all w3wp.exe and Click on "Attach" button

8. Now run the page that contains the webpart and click the your button(where i placed breakpoint)

in visual studio breakpoint is hitted so now am able to step through the code.