Wednesday, December 10, 2008

Edit document from dataview

<a href="{@FileRef}" onclick="return DispEx(this,event,'TRUE','FALSE','TRUE','SharePoint.OpenDocuments.3','0','SharePoint.OpenDocuments','','','','1','0','0','0x7fffffffffffffff')"><xsl:value-of select="@FileLeafRef" /></a>

Tuesday, December 2, 2008

Redirecting from NewForm.aspx to DispForm.aspx after creating a new item

public class CustomEventReceiver : SPItemEventReceiver
{
  private HttpContext _currentContext = null;
 
  public CustomEventReceiver () : base ()
  {
  if (null != HttpContext.Current)
  {
  _currentContext = HttpContext.Current
  }
  }
 
  public override void ItemAdding (SPItemEventProperties properties)
  {
  using(SPSite site = new SPSite(properties.SiteId))
  {
  using(SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
  {
  SPList list = web.Lists[properties.ListId];

  DisableEventFiring();
  SPListItem itemToAdd = list.Items.Add();
  foreach (SPField field in itemToAdd.Fields)
  {
  if (!field.Hidden && !field.ReadOnlyField && field != null && field.InternalName != "Attachments")
  {
  itemToAdd[field.InternalName] = properties.AfterProperties[field.InternalName];
  }
  }
  itemToAdd.Update();
  EnableEventFiring();


  // Redirect
  SPUtility.Redirect("/cn/Lists/Bills%20of%20Lading/DispForm.aspx?ID=" + itemToAdd.ID, SPRedirectFlags.Default, _currentContext);
  }
  }
  }
}

Thursday, November 20, 2008

Friday, November 14, 2008

JavaScript: Get URL Parameters

var _GET={};
for(var m, v=location.href.split(/[?&]/), k=v.length-1;k>0;k--)
_GET[(m=v[k].split(/[=#]/))[0].toLowerCase()] = m.length>1?decodeURI(m[1]):"";

example:

mypage.aspx?MyParameter=2

var param=_GET.myparameter; (always use lowercase)

Tuesday, November 11, 2008

focus field javascript

<script language="javascript" type="text/javascript">
_spBodyOnLoadFunctionNames.push("addAttributes");

function addAttributes() {
var control = document.getElementsByName("WPQ2accountsSearch");

if(control.length > 0)
{
control.item(0).focus();
}
}
</script>

Monday, October 20, 2008

401 Unauthorized, WebServices, Sharepoint

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(web.Site.ID))
{
// do things assuming the permission of the "system account"
}
});

Wednesday, October 15, 2008

Show/Hide control

control.style.display = 'block'; //show
control.style.display = 'none'; //hide

Set event onchange in JavaScript

document.getElementById("something").onchange = function(){hideFields()}

Javascript get dropdownlist Label value?

<select name="menu" id="menu"><option value="1">label 1</option><option value="2">label 2</option></select>

<button onclick="'show()'">Click me</button>

<script type="text/javascript"><br />function show(){<br />var theContents = document.getElementById('menu')[document.getElementById('menu').selectedIndex].innerHTML;<br />window.alert(theContents);<br />}<br /></script>

Friday, October 10, 2008

Access WebParts maintaince page

Append the querystring ?contents=1 

Wednesday, October 8, 2008

Speed Dial Tweaks In Opera: create more speed dial tabs

1. In opera address field type: opera:config

2. At the top is field for search. Type speed

3. Under "User Prefs" find "Speed Dial File" (speeddial.ini file location)

4. Go to that location and open file speeddial.ini for edit.

5. At top add:

[Size]
Rows=3
Columns=5

Tuesday, October 7, 2008

Code blocks are not allowed in this file

Whenever I start a new SharePoint project and put server side code in the master page I get this error. The fix to this is quite easy, all you need to do is locate the <pageparserpaths> </pageparserpaths> tags and put the following inbetween:

 <pageparserpath virtualpath="/_catalogs/masterpage/MyMasterPage.Master" compilationmode="Always" allowserversidescript="true">

Monday, September 29, 2008

Impersonation in SharePoint 2007

In SharePoint 2003 there was no easy way to use impersonation. In SharePoint 2007 there is a nice and easy build in way to use impersonation:

SPSite site = new SPSite("SiteCollection_Url");
SPWeb web = site.OpenWeb();
SPUser user = web.AllUsers["User_Name"]; 

SPUserToken token = user.UserToken;
SPSite impersonatedSiteCollection = new SPSite("SiteCollection_Url", token); 

You can also check whether an SPSite object is using impersonation:

SPSite siteCollection = SPControl.GetContextSite(HttpContext.Current);
bool impersonating = siteCollection.Impersonating;

Thursday, September 25, 2008

Enable Anonymous Access In SharePoint 2007 / WSS 3.0



  1. From Central Administration > Application Management > Application Security > Authentication Providers, select a Web application and the zone you want to modify. This is usually default.




  2. In the middle of the page, check Enable Anonymous Access and choose Save




  3. All site collections in that Web application can now have anonymous access enabled.




  4. Go to a site collection in the Web application you just enabled anonymous access for




  5. From Site Actions > Site Settings, open Advanced Permissions




  6. From the Settings drop-down menu, select Anonymous Access




  7. For this example, enable anonymous access for Lists and Libraries and click OK




  8. Browse to any document library in this site collection




  9. From the Settings drop-down menu, select Document Library Settings




  10. In the Permissions and Management column, select Permissions for this document library




  11. From the Actions menu, select Edit Permissions to break inheritance




  12. From the newly appeared Settings drop-down menu, select Anonymous Access




  13. Check View Items and click OK.



Wednesday, September 10, 2008

CAML - Query Lookup Field by ID; not by Value

When lookup fields are created, sharepoint stores them as
ID;#Value format in related list.

for e.g.
in list “Contacts” there is column called “Country”.
There is item in contact with value “India” assigned in Country, having item ID=10.

Now for another list “List2″ when column “Country” is stored as Lookup column named “refCountry”; and for certain data item “India” is selected from combo box, Sharepoint stores it in,

10;#India in “List 2″ -> “refCountry” column.

When having CAML query in List2

The typical query looks like
<Query>
<Where>
<Eq>
<FieldRef Name=”RefCountry” />
<Value Type=”Lookup”>India</Value>
</Eq>
</Where>
</Query>

The disadvantage of this way is If list Contacts is having more than one entry(item or row) having value India for Country it will return the only first one. This may give inconsistent data for further.

To avoid this query should be base on ID not by Value.
This can be achieved thru :

<Query>
<Where>
<Eq>
<FieldRef Name=”RefCountry” LookupId=”TRUE” />
<FieldRef Name=”RefCountry” LookupId=”TRUE” />
<Value Type=”Lookup”>10</Value>
</Eq>
</Where>
</Query>

where 10 is the ID for item having country=”India”

Hide fields in New, Display or View forms in SharePoint




One of the limiting factors in using the default forms (NewForm.aspx, EditForm.aspx) is that there is no obvious way to hide columns from appearing in the form.  By default all columns in a list or document library will appear in your forms. 


After doing quite a bit of research on this I found a fairly easy way to hide fields by using JavaScript within the form pages themselves.  In my research I found several different sets of JavaScript code, but some of the scripts are easier to implement than others.  Below I provide the best and most straight forward JavaScript and some simple steps to guide you along.


To hide fields in a SharePoint 2007 form, follow these steps (I will use the NewForm.aspx in my example)



  1. Open SharePoint Designer and navigate to the site that contains the list or document library you wish to customize.
  2. Expand the folder named “Forms” under the desired list or document library.  You should see about seven .aspx pages (AllItems.aspx, EditForm.aspx, NewForm.aspx, etc)
  3. Open the NewForm.aspx page and switch to the “code” view to edit the HTML of the page.
  4. Paste the JavaScript code immediately below the the following HTML tag <asp:Content ContentPlaceHolderId=”PlaceHolderMain” runat=”server”>  This will add the JavaScript to the HTML inside the content placeholder tag.  Note: be sure to include the entire script below, including the <script and </script> tags.
  5. Modify the “hidefields()” section of the JavaScript code to refer to each SharePoint list field name to hide.  For example, the code sample below will hide the SharePoint fields named Title, Document Link, and PublishDate    Notice that you do not need to worry about internal field names or field types like other JavaScript techniques, you simply need to know the name of the field.
  6. Save the changes.  Select “Yes” when prompted to “…customize the page from the site definition…”
  7. Test the form


 



<script language="javascript" type="text/javascript">

_spBodyOnLoadFunctionNames.push("hideFields");

function findacontrol(FieldName) {

var arr = document.getElementsByTagName("!");
// get all comments
for (var i=0;i < arr.length; i++ )
{
// now match the field name
if (arr[i].innerHTML.indexOf(FieldName) > 0)
{ return arr[i]; }
}
}

function hideFields() {

var control = findacontrol("Title");
control.parentNode.parentNode.style.display="none";
control = findacontrol("Document Link");
control.parentNode.parentNode.style.display="none";
control = findacontrol("PublishDate");
control.parentNode.parentNode.style.display="none";

}
</script>

Friday, September 5, 2008

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

I used query with left outer joins.

First i used Server Explorer to drag and drop my table, than i modified query.

When i draged and droped my table there was unique field (id) with key logo. Because of joins, query returned more than one unique id, so solution is:

Make this unique field (id) property "Unique" into False.

That solved my problem.

Tuesday, August 26, 2008

Embed a hyperlink into data view web part XSLT

URL: 

http://[server]/[site]/Lists/Open%20Positions/AllItems.aspx?View={84EEA2F5-121B-40B7-946F-0FA704A1DAA1}&FilterField1=Recruiter&FilterValue1=Hold

Convert it into:
http://www.blogger.com/Lists/Open%20Positions/AllItems.aspx?View=%7b84EEA2F5-121B-40B7-946F-%200FA704A1DAA1%7d&FilterField1=Recruiter&FilterValue1=Hold

I have manually transformed the first argument from: 

{84EEA2F5-121B-40B7-946F-0FA704A1DAA1}

to: 

%7b84EEA2F5-121B-40B7-946F-0FA704A1DAA1%7d 

(In this, the open brace transforms to %7b and the closing brace transforms to %7d)

Friday, August 22, 2008

The server could not complete your request. Contact your Internet service provider or Web server administrator to make sure that the server........

soap:ServerServer was unable to process request. ---> A Web Part or Web 
Form Control on this Web Part Page cannot be displayed or imported because 
it is not registered as safe on this site. You may not be able to open this 
page in an HTML editor that is compatible with Microsoft Windows SharePoint 
Services, such as Microsoft Office SharePoint Designer. To fix this page, 
contact the site administrator to have the Web Part or Web Form Control 

configured as safe.

Solution:

1. Open the site, for example http://mossportal/site/default.aspx
2. add "?contents=1" to the end of url, like this: http://mossportal/site/default.aspx?contents=1
3. You have a Web Part Page Maintenance and if you have some web part error delete it
4. Open the page with SharePoint Designer and all work correctly

Change Contents Types Display Form

SPD refuses to update the supporting files for a content type unless it is the first in the 'create new' list.

Solution:

1. Change content type to first in New list

2. Assign custom form

Do that with all content types you want

Sunday, August 10, 2008

Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, ... failed (CLR Procedure)

Solution:

1) ALTER DATABASE [Database Name] SET TRUSTWORTHY ON

2) Change the Permission Set of the CLR Code to External (Visual Studio's Project Properties)
 a) Properties > Database > Permissions Level
 b) Set to external