public static void UpdateReadPermissionLevel(SPWeb web){SPRoleDefinition role = web.RoleDefinitions["Read"];role.BasePermissions = role.BasePermissions | SPBasePermissions.BrowseDirectories;role.Update();}
Monday, August 8, 2011
Issue in LINQ to Sharepoint 2010
Tuesday, August 2, 2011
SP.ClientContext is null or not an object in ECMAScript
}
}
Monday, December 13, 2010
Fix some issues in SPGridView control
SharePoint has a little known web control called SPGridView. This control can give you a SharePoint list-like view of data from any data source you can bind to (from a database or other sources). It also has features to sort, filter and group the data in much the same way users can with SharePoint lists. For more information on how to use this, Paul Robinson has two great posts: SPGridView and SPMenuField: Displaying custom data through SharePoint lists Part 1 and Part 2. In addition, Robert Fridén has a good post on Filtering with SPGridView. However, there are two bugs that can be annoying and cause confusion to users:
- When both filtering and grouping are enabled, the first group title row sometimes disappears.
- When both sorting and filtering are enabled, the sort indicator arrow image will sometimes appear in the wrong column.
Working around either of these bugs requires you to create your own web control based on SPGridView and overriding the CreateChildControls method. Here is the full code listing for the solution to work around both bugs.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
namespace AdventuresInConsulting
{
public class GridViewControl : SPGridView
{
protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
// Due to a bug in SPDataView, we need to reset the private field previousGroupFieldValue so the
// first grouping row title will consistently show up
FieldInfo fieldInfo = typeof(SPGridView).GetField("previousGroupFieldValue",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (fieldInfo != null)
fieldInfo.SetValue(this, null);
int returnValue = base.CreateChildControls(dataSource, dataBinding);
// Fix up a bug in the SPGridView where it is putting the sort arrow indicator on the wrong
// column if AllowFiltering is true
if (this.AllowFiltering && this.HeaderRow != null && !string.IsNullOrEmpty(this.SortExpression))
{
List<string> filterFields = new List<string>(this.FilterDataFields.Split(','));
if (this.AllowGrouping) filterFields.Insert(0, string.Empty);
for (int i = 0; i < this.HeaderRow.Cells.Count; i++)
{
DataControlFieldHeaderCell cell = this.HeaderRow.Cells[i] as DataControlFieldHeaderCell;
if (cell != null)
{
// Find the sort image control
Image sortImage = null;
foreach (Control c in cell.Controls)
{
sortImage = c as Image;
if (sortImage != null && sortImage.ImageUrl == this.SortDirectionImageUrl)
break;
}
// If this field is filterable, make sure the menu image is correct
if (i < filterFields.Count && filterFields[i].Trim() != string.Empty)
{
// If there is also an image control, remove it (we'll add it back in the right place)
if (sortImage != null) cell.Controls.Remove(sortImage);
// Find the menu control and add or remove the image in there as needed
foreach (Control c in cell.Controls)
{
if (c is Microsoft.SharePoint.WebControls.Menu)
{
Microsoft.SharePoint.WebControls.Menu menu = c as Microsoft.SharePoint.WebControls.Menu;
if (this.SortExpression.Equals(this.Columns[i].SortExpression, StringComparison.InvariantCultureIgnoreCase))
{
// Make sure it has the sort indicator
menu.RightImageUrl = this.SortDirectionImageUrl;
menu.ImageTextSpacing = new Unit("2px", CultureInfo.InvariantCulture);
}
else if (menu.RightImageUrl == this.SortDirectionImageUrl)
{
// Make sure it doesn't have the sort indicator
menu.RightImageUrl = null;
menu.ImageTextSpacing = Unit.Empty;
}
}
}
}
else
{
if (this.SortExpression.Equals(this.Columns[i].SortExpression, StringComparison.InvariantCultureIgnoreCase))
{
// Make sure there is a sort image
if (sortImage == null)
{
sortImage = new Image();
sortImage.ImageUrl = this.SortDirectionImageUrl;
sortImage.Style[HtmlTextWriterStyle.MarginLeft] = "2px";
cell.Controls.Add(sortImage);
}
}
else if (sortImage != null)
{
// Remove the sort image if it exists
cell.Controls.Remove(sortImage);
}
}
}
}
}
return returnValue;
}
}
}Fixing the group title row disappearance
Making it so that the first group title row doesn't disappear is fairly simple. I noticed that this problem occurred if the filter or sort caused the first group in the new results to be the same as the last group from the previous results. This led me to look at how SPGridView was checking for the start of a new group. Sure enough, it was just checking to see if the value of the group field in the current row was different from the group field value the last time it detected a change. It saves the last group value in a field called previousGroupFieldValue. That value was being preserved between calls (presumably so that paging will work correctly). The easy fix is to reset the last group value. The problem was that the value was accessed through a private field. Of course, thanks to .NET reflection, we won't let that stand in our way. We just grab the FieldInfo for previousGroupFieldValue and then forcibly set the value.
NOTE: you may need to modify this slightly if paging is enabled. You probably only want to reset it if the page has not changed.
Fixing the sort indicator arrow image
Fixing the sort indicator arrow image is the more difficult of the two work arounds. I had hoped to recreate the CreateChildControls method of SPGridView and just avoid the image from coming up in the wrong place. However, it turns out that SPGridView calls its base class CreateChildControls method and there is no way in C# for a derived class to call its base's base class methods. So, rather than trying to recreate a whole chain of CreateChildControls methods, I just let SPGridView put the arrows where it wanted to and then fixed them up later.
The code loops through all of the column headers and looks for the sort indicator image either as a standalone Image control, or as the RightImageUrl of the column menu's title. We remove the image where it doesn't belong and add the image where it does belong.
Note on this bug: the issue arises because enabling grouping causes a hidden column to be added to the front of the grid and the code logic SPGridView uses to decide which column to put the filter indicator on is off by one. You'd think it'd be off to the left, but it is actually off to the right because whoever wrote the code knew there was a hidden column, but they overcompensated for it (kind of feels like being in a car with a kid learning to drive with power steering for the first time).
Referrence: http://blogs.msdn.com/b/valdon/archive/2009/03/11/how-to-work-around-bugs-in-the-spgridview-control.aspx
Tuesday, December 7, 2010
How to rename the Title column in the list definition?
You need to do this in the list definition’s schema.xml, not in the content type definition file. In fact, don’t mention the Title column in the content type. If you do, the Title column will appear twice in your list.
Here is what you should modify in your schema.xml:
<Fields>
<Field ID="{82642EC8-EF9B-478F-ACF9-31F7D45FBC31}"
Type="Computed"
Name="LinkTitle"
DisplayName="My Own Title" />
<Field ID="{BC91A437-52E7-49E1-8C4E-4698904B2B6D}"
Type="Computed"
Name="LinkTitleNoMenu"
DisplayName="My Own Title" />
<Field ID="{FA564E0F-0C70-4AB9-B863-0177E6DDD247}"
Type="Text"
Name="Title"
DisplayName="My Own Title"
Required="TRUE" />
<!-- Other fields declaration -->
</Fields>
Keep in mind that you will need those three columns declaration, one for the plain Title data, the others for the Title with link and menu which are displayed in the standard form. You can put your own text inside the DisplayName attribute.
Monday, June 28, 2010
WCF is hosted on SharePoint site
- Set site http://nuvm64
- Create some files as the follow:
- Open IRevert.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService
{
[ServiceContract]
public interface IRevert
{
[OperationContract]
void Revert(string listName, int listItemId);
[OperationContract]
void ChangeListName(string listName, string newListName);
}
}
- Open Revert.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService
{
using Microsoft.SharePoint.Client.Services;
using System.ServiceModel.Activation;
using Microsoft.SharePoint;
[BasicHttpBindingServiceMetadataExchangeEndpointAttribute]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class RevertService : IRevert
{
public void Revert(string listName, int listItemId)
{
SPList oList = SPContext.Current.Web.Lists[listName];
SPListItem oItem = oList.GetItemById(listItemId);
if (oItem.Versions.Count > 1)
{
oItem.Versions.Restore(1);
}
}
public void ChangeListName(string listName, string newListName)
{
SPList oList = SPContext.Current.Web.Lists[listName];
oList.Title = newListName;
oList.Update();
}
}
}
- Open Revert.svc
<%@ServiceHost Language="C#" Debug="true"
Service="WcfService.RevertService, $SharePoint.Project.AssemblyFullName$"
Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressBasicHttpBindingServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
- Open IE and go to http://nuvm64/_vti_bin/Revert.svc/MEX.
Tuesday, June 22, 2010
Open Dialog - SharePoint 2010
<script type="text/javascript">
function OpenDialog()
{
var options = SP.UI.$create_DialogOptions();
options.url = "http://bing.com";
options.width = 400;
options.height = 300;
options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
SP.UI.ModalDialog.showModalDialog(options);
}
function CloseCallback(result, target)
{
if(result === SP.UI.DialogResult.OK)
{
alert('You clicked OK');
}
}
</script>
<input type="button" value="Show Dialog" onclick="OpenDialog()" />
Sunday, June 20, 2010
Creat List Definition with changing LinkTitle's name to custom name
- Copy source code above to this file.
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Field ID="{794E53F1-772B-4DE8-9186-172E833A2F25}" Name="StreetAddress" StaticName="StreetAddress" Group="TimeSheet Custom Column" DisplayName="Street Address" Required="TRUE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE" />
<Field ID="{C0448169-6DE9-499D-B8F9-47EFDF449DDA}" Name="City" StaticName="City" Group="TimeSheet Custom Column" DisplayName="City" Required="TRUE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<Field ID="{8443DDA9-FE75-4191-AF26-2452655B5F65}" Name="Country" StaticName="Country" Group="TimeSheet Custom Column" DisplayName="Country" Required="TRUE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<Field ID="{DC875049-CC83-4295-8101-05FCC43BE431}" Name="ZipCode" StaticName="ZipCode" Group="TimeSheet Custom Column" DisplayName="Zip Code" Required="FALSE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<Field ID="{48795933-77C1-4E27-BF36-473517BB59F1}" Name="Phone" StaticName="Phone" Group="TimeSheet Custom Column" DisplayName="Phone" Required="FALSE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<Field ID="{61021EA8-4B92-4C6B-99CB-40C68E774182}" Name="Fax" StaticName="Fax" Group="TimeSheet Custom Column" DisplayName="Fax" Required="FALSE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<Field ID="{F4AE9B55-FA6E-430E-B111-BA193B54E4FE}" Name="Email" StaticName="Email" Group="TimeSheet Custom Column" DisplayName="Email" Required="FALSE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<!--<Field ID="{22D96EE2-FCCE-47F0-9A78-92EECB14E198}" Name="Status" StaticName="Status" Group="TimeSheet Custom Column" DisplayName="Status" Required="TRUE" Type="Choice" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE">
<CHOICES>
<CHOICE>None</CHOICE>
<CHOICE>Active</CHOICE>
</CHOICES>
</Field>-->
<ContentType
ID="0x010089E3E6DB8C9B4B3FBB980447E313CE95"
Name="TSClient" Group="TimeSheet Custom Content Types" Description="TSClient" Version="0">
<FieldRefs>
<FieldRef ID="{794E53F1-772B-4DE8-9186-172E833A2F25}"/>
<FieldRef ID="{C0448169-6DE9-499D-B8F9-47EFDF449DDA}"/>
<FieldRef ID="{8443DDA9-FE75-4191-AF26-2452655B5F65}"/>
<FieldRef ID="{DC875049-CC83-4295-8101-05FCC43BE431}"/>
<FieldRef ID="{48795933-77C1-4E27-BF36-473517BB59F1}"/>
<FieldRef ID="{61021EA8-4B92-4C6B-99CB-40C68E774182}"/>
<FieldRef ID="{F4AE9B55-FA6E-430E-B111-BA193B54E4FE}"/>
<!--<FieldRef ID="{22D96EE2-FCCE-47F0-9A78-92EECB14E198}"/>-->
</FieldRefs>
</ContentType>
<!-- Do not change the value of the Name attribute below. If it does not match the folder name of the List Definition project item, an error will occur when the project is run. -->
<ListTemplate
Name="TSClient"
Type="10001"
DisallowContentTypes="FALSE"
BaseType="0"
OnQuickLaunch="TRUE"
SecurityBits="11"
Sequence="410"
DisplayName="TSClient"
Description="TSClient"
Image="/_layouts/images/itgen.png"/>
</Elements>
- Open Schema.xml file
- Copy them to this file
<?xml version="1.0" encoding="utf-8"?>
<List xmlns:ows="Microsoft SharePoint" EnableContentTypes="TRUE" Title="TSClient" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/TSFeatures-TSClient" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/">
<MetaData>
<ContentTypes>
<!--TimeSheet area start-->
<ContentTypeRef ID="0x010089E3E6DB8C9B4B3FBB980447E313CE95"/>
<!--TimeSheet area end-->
<ContentTypeRef ID="0x01">
<Folder TargetName="Item" />
</ContentTypeRef>
<ContentTypeRef ID="0x0120" />
</ContentTypes>
<Fields>
<!--TimeSheet area start-->
<Field ID="{82642EC8-EF9B-478F-ACF9-31F7D45FBC31}" Type="Computed" Name="LinkTitle" DisplayName="Name" />
<Field ID="{BC91A437-52E7-49E1-8C4E-4698904B2B6D}" Type="Computed" Name="LinkTitleNoMenu" DisplayName="Name" />
<Field ID="{FA564E0F-0C70-4AB9-B863-0177E6DDD247}" Type="Text" Name="Title" DisplayName="Name" Required="TRUE" />
<Field ID="{794E53F1-772B-4DE8-9186-172E833A2F25}" Name="StreetAddress" StaticName="StreetAddress" Group="TimeSheet Custom Column" DisplayName="Street Address" Required="TRUE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE" />
<Field ID="{C0448169-6DE9-499D-B8F9-47EFDF449DDA}" Name="City" StaticName="City" Group="TimeSheet Custom Column" DisplayName="City" Required="TRUE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<Field ID="{8443DDA9-FE75-4191-AF26-2452655B5F65}" Name="Country" StaticName="Country" Group="TimeSheet Custom Column" DisplayName="Country" Required="TRUE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<Field ID="{DC875049-CC83-4295-8101-05FCC43BE431}" Name="ZipCode" StaticName="ZipCode" Group="TimeSheet Custom Column" DisplayName="Zip Code" Required="FALSE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<Field ID="{48795933-77C1-4E27-BF36-473517BB59F1}" Name="Phone" StaticName="Phone" Group="TimeSheet Custom Column" DisplayName="Phone" Required="FALSE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<Field ID="{61021EA8-4B92-4C6B-99CB-40C68E774182}" Name="Fax" StaticName="Fax" Group="TimeSheet Custom Column" DisplayName="Fax" Required="FALSE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<Field ID="{F4AE9B55-FA6E-430E-B111-BA193B54E4FE}" Name="Email" StaticName="Email" Group="TimeSheet Custom Column" DisplayName="Email" Required="FALSE" Type="Text" MaxLength="255" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<!--<Field ID="{22D96EE2-FCCE-47F0-9A78-92EECB14E198}" Name="Status" StaticName="Status" Group="TimeSheet Custom Column" DisplayName="Status" Required="TRUE" Type="Choice" ShowInDisplayForm="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE">
<CHOICES>
<CHOICE>None</CHOICE>
<CHOICE>Active</CHOICE>
</CHOICES>
</Field>-->
<!--TimeSheet area end-->
</Fields>
<Views>
<View BaseViewID="0" Type="HTML" MobileView="TRUE" TabularView="FALSE">
<Toolbar Type="Standard" />
<XslLink Default="TRUE">main.xsl</XslLink>
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
<FieldRef Name="LinkTitleNoMenu"></FieldRef>
</ViewFields>
<Query>
<OrderBy>
<FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
</OrderBy>
</Query>
<ParameterBindings>
<ParameterBinding Name="AddNewAnnouncement" Location="Resource(wss,addnewitem)" />
<ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
<ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_ONET_HOME)" />
</ParameterBindings>
</View>
<View BaseViewID="1" Type="HTML" WebPartZoneID="Main" DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;" DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/generic.png" Url="AllItems.aspx">
<Toolbar Type="Standard" />
<XslLink Default="TRUE">main.xsl</XslLink>
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
<FieldRef Name="Attachments"></FieldRef>
<FieldRef Name="LinkTitle"></FieldRef>
<!--TimeSheet area start-->
<FieldRef Name="StreetAddress" DisplayName="Street Address" />
<FieldRef Name="City" />
<FieldRef Name="Country" />
<FieldRef Name="ZipCode" DisplayName="Zip Code"/>
<FieldRef Name="Phone" />
<FieldRef Name="Fax" />
<FieldRef Name="Email" />
<!--<FieldRef Name="Status"/>-->
<!--TimeSheet area end-->
</ViewFields>
<Query>
<OrderBy>
<FieldRef Name="ID"></FieldRef>
</OrderBy>
</Query>
<ParameterBindings>
<ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
<ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_DEFAULT)" />
</ParameterBindings>
</View>
</Views>
<Forms>
<Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
<Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
<Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
</Forms>
</MetaData>
</List>
- Notice:
+ I used this code to change "Title"(default) of LinkTitle to "CustomName"
<Fields>
<Field ID="{82642EC8-EF9B-478F-ACF9-31F7D45FBC31}" Type="Computed" Name="LinkTitle" DisplayName="CustomName" />
<Field ID="{BC91A437-52E7-49E1-8C4E-4698904B2B6D}" Type="Computed" Name="LinkTitleNoMenu" DisplayName="CustomName" />
<Field ID="{FA564E0F-0C70-4AB9-B863-0177E6DDD247}" Type="Text" Name="Title" DisplayName="CustomName" Required="TRUE"/>
...
</Fields>