Update Farm Properties From Any SP Web Application Inside The Farm

by Mete Bulutay 10. April 2012 13:52

If you call SPFarm.Update() in a web application other than the central admin web application, you will probably face with an error message indicating "acess denied" or something similar. This means you can't set farm properties from a web application in the farm. Really?

No, thanks to Mor Shemesh's blog post, it is possible to set your farm to allow farm updates outside central admin web application. (There is no need to indicate the user account must have farm administrator priviledges). Run Sharepoint management shell and enter following lines, and finally an iisreset...

 

$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$contentService.RemoteAdministratorAccessDenied = $false
$contentService.Update()

 

Tags: , ,

Sharepoint

Programatically Creating New List From Custom List Definition

by Mete Bulutay 26. March 2012 16:23

After spending several hours trying to figure out why recommended methods on blogs do not work, I finally found out a way to programatically create a list from custom list definition. The key is, not forgetting to assign a type id to the custom list definition like follows:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <!-- 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="MyListDefinition"
        Type="10081"
        BaseType="0"
        OnQuickLaunch="FALSE"
        SecurityBits="11"
        Sequence="410"
        DisplayName="My Custom Definition"
        Description="My Custom Definition"
        Image="/_layouts/images/itgen.png"/>
</Elements>

Please note that Type="10081" should be unique for the list definition.

After assigning an ID to the list definition it's easy at least possible to create a custom list programatically with the overload of SPWeb.Lists.Add(web) method that gets type id as a parameter.

Guid listId = web.Lists.Add(
                "My List", 
                "Description for My List", 
                "Lists/MyList",
                "A86128A3-F0DE-46DA-A653-F8998AE9F59F",
                10081,
                "100");
            web.Update();

Note that the Guid value which is A86128A3-F0DE-46DA-A653-F8998AE9F59F in the sample code should be the id of the feature that activates the custom list definition. (You do not have to create a seperate feature to activate the list definition, you may use the same feature for other stuff too. There is no such a restriction.)

Tags: ,

Sharepoint

Sharepoint: Check If List Instance Already Exists

by Mete Bulutay 22. March 2012 17:33

List Instances is a cool feature of Visual Studio that lets you develop deployable solutions. Here is the thing: what happens if you deploy a wsp containing a list instance to a location which already contains the same list instance? Does it override or ignore?

In order to define this behaviour in case of a conflict, you have to edit SharepointProjectItem.spdata file located in the same folder with your list instance definition XML (Click "show files" button in VS Solution Explorer if you can't see the file). Set DeploymentConflictResolutionBehavior to None if you want deployment to ignore exitsting instance of a list in your package.

<?xml version="1.0" encoding="utf-8"?>
<ProjectItem Type="Microsoft.VisualStudio.SharePoint.ListInstance" DefaultFile="Elements.xml" SupportedTrustLevels="All" SupportedDeploymentScopes="Web, Site" xmlns="http://schemas.microsoft.com/VisualStudio/2010/SharePointTools/SharePointProjectItemModel">
  <Files>
    <ProjectItemFile Source="Elements.xml" Target="MyListInstance\" Type="ElementManifest" />
  </Files>
  <ExtensionData>
    <ExtensionDataItem Key="DeploymentConflictResolutionBehavior" Value="None" />
  </ExtensionData>
</ProjectItem>

Tags: ,

Sharepoint

Programatically Modifying Web.Config in Sharepoint 2010

by Mete Bulutay 14. March 2012 00:00

If you are developing a generic Sharepoint solution that would be installed to different Sharepoint instances and need some custom web.config modifications, you need to do this programatically. In fact, even in single server scenarios, modifying web.config programatically, instead of copying a pre-configured one is a best practice.

To do that, you may use SPWebConfigModification class providedby Sharepoint object model in Sharepoint.Administration namespace. Here is a code snippet taken from msdn (modified just a little bit) in order to add a safe control entry to web.config. Please note that Name property has to match with the xml entry and then Type property is set to EnsureChildNodes, this snippet can also update existing instances in web.config.

SPWebService myService = SPWebService.ContentService;

SPWebConfigModification myModification = new SPWebConfigModification();

// This should be the path of modification in web.config
myModification.Path = "configuration/SharePoint/SafeControls";

// This has to match with Value property in order to find the correct entry when updating
myModification.Name = "SafeControl[@Assembly='MyCustomAssembly'][@Namespace='MyCustomNamespace'][@TypeName='*'][@Safe='True']";
myModification.Sequence = 0;

// Just set this to some string you desire, no user acc is required
myModification.Owner = "CodeHolio";

// This option also provides update functionality
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;

// This is the actual xml string of the modification
myModification.Value = "<SafeControl Assembly='MyCustomAssembly' Namespace='MyCustomNamespace' TypeName='*' Safe='True' />";

myService.WebConfigModifications.Add(myModification);
myService.Update(); 
myService.ApplyWebConfigModifications();

You may find some extra information on issues of this class in MOSS 2007 from Reza Alirezaei's blog. I haven't figured out yet which ones of them still exist.

Tags: ,

Sharepoint

How To Make StyleCop Ignore Generated Code

by Mete Bulutay 20. February 2012 11:28

Ok, StyleCop is a nice tool for coding standarts, but in can be a pain especially when working with Sharepoint. There are three basic ways to make it ignore partial code or the whole code file.

  • Put the code to be ignored into a region with name that contains "generated code"
  • Put //<auto-generated /> comment at the beginning of the file (the whole file will be ignored)
  • Put .Designer.cs at the end of the name of the file (the whole file will be ignored)

Tags:

.Net | Sharepoint

Relative Path Issue in Sharepoint Branding

by Mete Bulutay 23. December 2011 13:07

While applying branding to Sharepoint site hierarchy on a managed path (e.g. "http://yourserver/sites/sitecol1"), one of the most common issue that you will face with is not being able to get correct relative urls to resources (like css files) refenced in master page. This happens beacuse when you reference your css file with a relative path like "/Style%20Library/CustomStyles/style1.css" from the master page and a subsite at URL "http://yourserver/sites/sitecol1/subsitex" uses that master page, it will look for the file at url: "http://yourserver/Style%20Library/CustomStyles/style1.css" or in its local Style Library at url: "http://yourserver/sites/sitecol1/subsitex/Style%20Library/CustomStyles/style1.css". The second action may occur if the relative path is written without "/" at the beginning. Either case, you will not be able to reach the css located in style library of root site with a relative url. And paths like "../../../../../sites/sitecol1..." or providing full path are not good execises. Instead, you get use of Sharepoint's cool SPUrl feature. Usage is simple:

In Master Page:

<link id="css1" runat="server" rel="stylesheet" type="text/css" href="<%$ SPUrl:~SiteCollection/StyleLibrary/CustomStyles/style1.css %>" />

Note that runat="server" attribute is required. You can use this syntax in other html tags too. For paths defined in CSS you should use relative paths to the CSS's location. For eaxample for an image located in "http://yourserver/sites/sitecol1/Style%20Library/StyleImages/img.jpg" you should use "../StyleImages/img.jpg"

Tags: , ,

Sharepoint

Get 14 Hive Path in Object Model

by Mete Bulutay 8. December 2011 15:27

SPUtility.GetGenericSetupPath(<SubDirName>) gets the system path of 14 hive.

Note: It contains trailing "\" ;)

Tags:

Sharepoint

Modifying .stp File Contents and Repackaging

by Mete Bulutay 6. December 2011 16:41

Check out this link for modifying site template (.stp) file and repackage it with modified content.

Tags: ,

Sharepoint

Windows Communication Foundation Articles

by Mete Bulutay 22. November 2011 17:01

Tags:

WCF

Entity Framework 4.1 Articles

by Mete Bulutay 22. November 2011 16:24

Tags:

Entity Framework