Showing posts with label SharePoint. Show all posts
Showing posts with label SharePoint. Show all posts

Friday, November 4, 2011

Remove Lookup Hyperlink from SharePoint List view using JQuery

When you use lookup to the list automatically SharePoint give link to the lookup item on list view.
Check it out….
Lookup column with Hyperlinks
But many times, we don’t want  those Hyperlinks on the list view, as we can remove it from SharePoint Designer but it’s a hectic task.
Simply add JQuery  to remove those Hyperlinks.
First, get the URL of the link using Developer tool of IE or Firebug of Firefox browser. we are going to some part of URL in the below code.

Get URL using Developer tool of IE


Then copy following JQuery code to the HTML form WebPart on list view.

Jquery For MOSS 2007/WSS 3.0:


    
    

Note: Change highlighted text as per your context.
Jquery For SharePoint 2010/SharePoint Foundation 2010:





Note: Change highlighted text as per your context.

If View is Grouped, set list view setting "By default, show groupings:" to  Expanded


List View setting: Make sure that By default, show groupings: Expanded

List view with removed hyperlinks to Lookup columns:

Live simple!

Tuesday, September 27, 2011

Print Button on SharePoint Page


We can easly add print functionality on SharePoint Page. Just add following Javascript on the page (you can use HTML Form Webpart for adding javascript).

In Javascript i have used WebPartElementID="onetIDListForm" ; which is SharePoint Default ID of the Main contents.

JavaScript:

<script type="text/javascript" language="JavaScript">
//Controls which Web Part or zone to print 'TD ID'
var WebPartElementID = "onetIDListForm"; //SharePoint Main Content ID

//Function to print Web Part
function PrintArea()
{
var bolWebPartFound = false;
if (document.getElementById != null)
{
//Create html to print in new window
var PrintingHTML = '<HTML>\n<HEAD>\n';
//Take data from Head Tag

PrintingHTML += document.getElementsByTagName('head')[0].innerHTML;
PrintingHTML += '\n</HEAD>\n<BODY class="printSQS">\n';
var WebPartData = document.getElementById(WebPartElementID);
if (WebPartData != null)
{
PrintingHTML += WebPartData.innerHTML;
bolWebPartFound = true;
}
else
{
bolWebPartFound = false;
alert ('Cannot Find Web Part');
}
}
PrintingHTML += '\n</BODY>\n</HTML>';
//Open new window to print
if (bolWebPartFound)
{
var PrintingWindow = window.open("","PrintWebPart",
"toolbar,width=800,height=600,scrollbars,resizable,menubar");
PrintingWindow.document.open();
PrintingWindow.document.write(PrintingHTML);
PrintingWindow.document.close();
// Open Print Window
PrintingWindow.print();
}
}
</script>

HTML Code:

<input type="button" value="Print" onclick="javascript:void(PrintArea());return false;__doPostBack('btPrint','')" />

<a href="#" onclick="javascript:void(PrintArea());return false;__doPostBack('btPrint','')"  ><img src="/sites/aaa/PublishingImages/print.gif" border="0" complete="complete"/></a>


You can add html code any where on the page.

Thursday, April 14, 2011

Thursday, February 17, 2011

Naming Best Practice in SharePoint

There are different Naming conventions in SharePoint. Avoid using space or Special characters like [! @ # $ % ^ & * ( ) _ + ? < > : ; ” ’ { } [ ] \ | / .]. When naming your Sites, Lists, Document Libraries, columns, views, files, folders, avoid these characters. Underscore [_] will be allowed if used in between two words but still not recommended and Underscore is to be avoided as the first character in the name, and multiple consecutive periods should be avoided. In columns and document library names, if you think it’s should be relevant or good looking then go for renaming the column or document library name afterwards, getting a nice looking name combined with a coder-friendly URL .
Good URL’s: http://example.com/pages/ NewPost.aspx http://example.com/pages/ NewPost.aspx? PostID=123&Command=Edit http://example.com/sites/InformationManagment/

Partially Good URL’s: http://example.com/pages/ New-Post.aspx? PostID=123&Command=Edit http://example.com/sites/Information_Managment/ NewPost.aspx

Bad URL’s: http://example.com/sites/Information%20Managment/ 

Saturday, September 25, 2010

Creating a List Item programmatically using the object model

The below code shows how to create a List Item programmatically using the object model in SharePoint 2007.

Adding a List Item to a Custom List

Unable to find source-code formatter for language: csharp. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
using (SPWeb web = siteCollection.AllWebs["webname"])
{
  SPList list = web.Lists["Custom List"];
  SPListItem item = list.Items.Add();
  item["Title"] = "New List Item";
  item.Update();
}

Optimised Adding a List Item to a Custom List

Use the following to add an item to a list:
Unable to find source-code formatter for language: csharp. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
01: public static SPListItem OptimizedAddItem(SPList list)
02: {
03: const string EmptyQuery = "0";
04: SPQuery q = new SPQuery {Query = EmptyQuery};
05: return list.GetItems(q).Add();
06: }
Do not use SPList.Items.Add as this will get all items in the list before adding a new SPListItem.
Source: Aidan Garnish

Adding a List Item to a Custom List with an Attachment

Unable to find source-code formatter for language: csharp. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
using (SPWeb web = siteCollection.AllWebs["webname"])
{
  SPList list = web.Lists["Custom List"];
  SPListItem item = list.Items.Add();
  item["Title"] = "New List Item";

  SPAttachmentCollection attachments = item.Attachments;
  attachments.Add(fileName, byteArrayContents);

  item.Update();
}

Adding a List Item to a Publishing Page Library

using (SPSite site = new SPSite("http://moss"))
{
    using (SPWeb web = site.OpenWeb())
    {
        PublishingSite pSite = new PublishingSite(site);
        SPContentType ctype = pSite.ContentTypes["Welcome Page"];
        PageLayoutCollection pageLayouts = pSite.GetPageLayouts(ctype, true);
        PageLayout pageLayout = pageLayouts.FirstOrDefault<PageLayout>(p => p.Name == "WelcomeSplash.aspx");
        PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
        PublishingPageCollection pPages = pWeb.GetPublishingPages();
        PublishingPage pPage = pPages.Add("Programmatic_Test.aspx", pageLayout);
        SPListItem newpage = pPage.ListItem;
        newpage["Title"] = "Page added programmatically";
        newpage.Update();

        newpage.File.CheckIn("all looks good");
        newpage.File.Publish("all looks good");
    }
}
NOTE: requires .NET 3.5 for System.Linq FirstOrDefault method, can be switched for a loop.
Source: sridhara

Adding a List Item to a Document Library with an attachment

Unable to find source-code formatter for language: csharp. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
  // Creates document in given list (root folder).
// Returns true if the file was created, false if it already
// exists or throws exception for other failure
protected bool CreateDocument( string sFilename, string sContentType, string sList)
{
    try
    {
        SPSite site = SPContext.Current.Site;

        using (SPWeb web = site.OpenWeb())
        {
            SPList list = web.Lists[sList];
            // this always uses root folder
            SPFolder folder = web.Folders[sList];
            SPFileCollection fcol = folder.Files;

            // find the template url and open
            string sTemplate = list.ContentTypes[sContentType].DocumentTemplateUrl;
            SPFile spf = web.GetFile(sTemplate);
            byte[] binFile = spf.OpenBinary();
            // Url for file to be created
            string destFile = fcol.Folder.Url + "/" + sFilename;

            // create the document and get SPFile/SPItem for
            // new document
            SPFile addedFile = fcol.Add(destFile, binFile, false);

            SPItem newItem = addedFile.Item;
            newItem["ContentType"] = sContentType;
            newItem.Update();
            addedFile.Update();
            return true;
        }
    }
    catch (SPException spEx)
    {
        // file already exists?
        if (spEx.ErrorCode == -2130575257)
            return false;
        else
            throw spEx;
    }
}
This code:
1. Gets a SPSite for the current site collection, and opens an SPWeb for the site.
2. Gets a SPList for the given list, and then an SPFolder for the root folder in this list. This code always creates the document in the root folder, but the code can easily be changed to place the document in any folder in the document library.
3. Gets a SPFileCollection for the documents in the folder.
4. "DocumentTemplateUrl" is used to return the Url of document template associated with the given content type.
5. Get an SPFile for the document template in spf and open it for binary access using OpenBinary
6. Add a new document to the folder through the SPFileCollection referenced by fcol.
7. Get an SPItem for the new document and set the "ContentType" column to ensure it uses the correct content type (it will default to the first content type in the document library).
8. Update the item and the added file.
9. The catch section checks for an -2130575257 error, which indicates the file already exists.

Adding a List Item to a Calendar List

Unable to find source-code formatter for language: csharp. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
SPList cal = site.Lists["Calendar"];
SPListItem calEvent = cal.Items.Add();
calEvent["Title"] = "Frequent Event";
string recurrence = "<recurrence><rule>" +
"<firstDayOfWeek>su</firstDayOfWeek>" +
"<repeat><daily dayFrequency='2?/></repeat>" +
"<windowEnd>2010-09-20T09:00:00Z</windowEnd>" +
"</rule></recurrence>";
calEvent["RecurrenceData"] = recurrence;
calEvent["EventType"] = 1;
calEvent["EventDate"] = new DateTime(2009, 1, 26, 8, 0, 0);
calEvent["EndDate"] = new DateTime(2009, 1, 26, 9, 0, 0);
calEvent["UID"] = System.Guid.NewGuid();
calEvent["TimeZone"] = 13;
calEvent["Recurrence"] = -1;
Note that the CAML to create the recurrence can be complicated so please take a look at the MSDN article.

Adding a List Item to a List in SharePoint 2010

In SharePoint 2010 you can use the following code to add an item to a SPList in an optimized way:
Unable to find source-code formatter for language: csharp. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
using (SPWeb web = SPContext.Current.Web)
{
  SPList list = web.GetList(string.concat(web.Url, "/Lists/Custom List"));
  SPListItem item = list.AddItem();
  item["Title"] = "New List Item";
  item.Update();
}

Wednesday, June 2, 2010

How to View Detail Error Message in SharePoint

Many times when we do SharePoint Custom programming ,Boom happen's "An unexpected error occurred".

So how to know the exact cause of error.........
You have to modify your web application Web.Config file as follows...

1. CallStack=”false” changed to CallStack=”true”

2. <customErrors mode=”On” /> changed to <customErrors mode=”Off” />

Go ahead.............

Thursday, December 17, 2009

Can't set the selected item of the dropdownlist in sharepoint

Question: Can't set the selected item of the dropdownlist in sharepoint


Answer::


Mark EnableViewState property of page directive to true in .aspx page



<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="CodeBehindAmit.ShowList" EnableViewState="true" EnableViewStateMac="true" AutoEventWireup="true" %>