torsdag 5. desember 2013

Override values of Created By and Modified By fields

In some cases, it could be useful (or maybe even necessary) to be able to override the values in a SPListItem's Created By (Author as Internal Name) and Modified By (Editor as Internal Name).

So, how can you accomplish this? Simple!


web.AllowUnsafeUpdates = true;

item["Editor"] = string.Format("{0};#{1}", spUser.ID, spUser.Name);
item["Author"] = string.Format("{0};#{1}", spUser.ID, spUser.Name);

item.UpdateOverwriteVersion();

web.AllowUnsafeUpdates = false;


The key is item.UpdateOverwriteVersion(). item.Update() and item.SystemUpdate() won't work here. Created By and Modified By are read-only fields and aren't meant to be messed with. However, I have been in situations where I had to modify them using code.

----
Horns up \m/

torsdag 29. august 2013

Hide SharePoint 2010 controls from anonymous users

I was recently working on a SharePoint 2010 Internet-facing website for a client and needed to hide the Ribbon from anonymous users. I tried various approaches to achieve this, but finally I came over a great blog post (http://blogs.msdn.com/b/zwsong/archive/2010/04/29/how-to-hide-ribbon-from-users-without-edit-page-privilege.aspx) which introduced an ingenious approach on this.

The idea is very simple really. Make usage of the SPSecurityTrimmedControl  control, but instead of wrapping the whole Ribbon DIV inside it, hide the Ribbon DIV and wrap SPSecurityTrimmedControl around a small JavaScript that shows the Ribbon.

More technical how-to (slightly modified from the original post to fit my needs):

  1. Open SharePoint Designer and open your site and your Master Page.
  2. Locate:
    <div id="s4-ribbonrow" class="s4-pr s4-ribbonrowhidetitle">
  3. Change it to:
    <div id="s4-ribbonrow" class="s4-pr s4-ribbonrowhidetitle" style="display:none;">
    1. >
  4. Find the end tag of "s4-ribbonrow" and add following code after it:
    <Sharepoint:SPSecurityTrimmedControl ID="spTrimRibbon"runat="server" PermissionMode="All" PermissionContext="CurrentSite" AuthenticationRestrictions="AuthenticatedUsersOnly">
        <script type="text/javascript">        document.getElementById("s4-ribbonrow").style.display = "block";
        </script>
    </
    Sharepoint:SPSecurityTrimmedControl>
  5. Save your work.
  6. That's it!
Now, this approach can be used to hide/show elements based on various rules, not just the Ribbon as illustrated in this post.

Hope you found this as useful as I did!

Thanks to Dr.Z for the original post.