tag:blogger.com,1999:blog-75893112564180177872024-02-08T06:55:56.391-08:00Coffee and SharePointsusanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.comBlogger32125tag:blogger.com,1999:blog-7589311256418017787.post-90988978560263028672013-02-11T13:44:00.000-08:002013-02-11T13:45:23.581-08:00SharePoint Form Control People Picker Has Problems Resolving User ValueThis latest issue is exactly what it says on the tin :) You have a People Picker control which is used to story a value that corresponds to an SPFieldUserValue in a SharePoint list object. Because there is no instant conversion between the value in the People Picker you perform a conversion:<br />
<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">string userValue = userCtrl.Value.ToString();</span><br />
<span style="font-family: Courier New, Courier, monospace;">int pos = userValue.IndexOf("#");</span><br />
<span style="font-family: Courier New, Courier, monospace;">userValue = userValue.Substring(pos + 1);</span><br />
<br />
(there may well be more elegant ways to get the value out of the People Picker, by all means go out and find them.)<br />
<br />
This should extract the part of the uservalue that reads "DOMAIN\username_s" which is what we need to create the SPUser object<br />
<br />
<span style="font-family: Courier New, Courier, monospace;">SPUser user = web.EnsureUser(userValue);</span><br />
<br />
<span style="font-family: Verdana, sans-serif;">I run this for user X and it works perfectly. All good. Then another tester notes that when she enters user Y and clicks Submit, the page doesn't update properly. After a bit of fiddling I discover that in the case of this particular user, the Value property on the People Picker control returns a number, that is, the SharePoint user ID. It is not possible to use this as a parameter for the EnsureUser method, so we are in error city.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">So naturally I wondered, why is it behaving for user X and not for user Y. Did a bit of googling and came up with <a href="http://social.technet.microsoft.com/Forums/en/sharepointadmin/thread/cf075a36-c28b-4134-a708-3b5b3b959bf6" target="_blank">this</a>. It appears to have something to do with the user list in SharePoint. So I went into that user, clicked the Edit User and saved without changing anything. Reran the code. Still had the same problem.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">This is how I fixed it in the code - but I still don't know why it didn't work for that user but did for every single other user I tried. So any suggestions on that front welcome.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="line-height: 1.2em; outline: none;"><span style="font-family: Arial, sans-serif;"><span style="font-size: x-small;"> </span> </span><span style="font-family: Courier New, Courier, monospace;">int SPId;</span></span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; line-height: 1.2em; outline: none;"> SPUser managerUser = null;</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; line-height: 1.2em; outline: none;"> if (Int32.TryParse(managerValue.Trim(), out SPId) == true)</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; line-height: 1.2em; outline: none;"> {</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; line-height: 1.2em; outline: none;"> managerUser = web.SiteUsers.GetByID(SPId);</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; line-height: 1.2em; outline: none;"> }</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; line-height: 1.2em; outline: none;"> else</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; line-height: 1.2em; outline: none;"> { </span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; line-height: 1.2em; outline: none;"> managerUser = web.EnsureUser(managerValue);</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; line-height: 1.2em; outline: none;"> }</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; line-height: 1.2em; outline: none;"><br /></span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; line-height: 1.2em; outline: none;"><br /></span></div>
<br />susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-82253310638796787012013-02-05T12:21:00.003-08:002013-02-05T12:22:27.431-08:00For Those Wishing to Enable / Disable / Set Emails for Site Access Requests in an entire Site Collectionmight I recommend <a href="http://mallen22842.blogspot.ie/2013/02/i-dont-want-people-requesting-access-to.html?showComment=1360093114638#c5833940977827005035" target="_blank">this script</a> from <a href="http://mallen22842.blogspot.ie/" target="_blank">Michael Allen</a>, which does the job with aplomb!susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-12265307163450458052013-02-05T11:55:00.000-08:002013-02-05T12:11:15.499-08:00It's Always OK To Refuse To Entertain TrollsA brief pause from the usual outpourings of code and blather to talk about a serious topic.<br />
<br />
A few people in an online SharePoint comm to which I was recently invited expressed mild disagreement with <a href="http://coffeeandsharepoint.blogspot.ie/2013/01/customising-newformaspx-of-list-using.html" target="_blank">my last post</a>. Probably because it is safer to use JQuery and easier to maintain for administrators. The post merits debate and criticism, I agree. It could have been an opportunity for discussion. But.<br />
<br />
One person trolled me. He was negative, provocative and told me I did not know what I was talking about. I told him that I would not entertain responses that were uncivil, as his was. After all, I didn't know this man from Adam and he had never responded to any of my posts before (presumably I had not yet done anything to give him excuse to find fault, so affording him little pleasure.)<br />
<br />
In response I got a big pile of rage. The gist of same being that I needed to learn humility and take criticism.<br />
<br />
One tiny problem: I cannot take criticism where none has been given.<br />
<a name='more'></a>No link, no technology, no reasons why, nothing. Just rage from start to finish.<br />
<br />
Real criticism says, even briefly, what is wrong and why - and if there is time for the critic to suggest a fix, it contains that too. It may be trenchant. But emotional abuse is not there to criticise or suggest. It is there to offend, provoke and get a kick of some sort. What he was doing was not criticism. It was a game to provide him with something he lacked in himself.<br />
<br />
For anyone who has experienced this: <b>it is always OK not to entertain a troll</b>, no matter who they are or what their level of mastery might be, and even if your work merits criticism, as mine does.<br />
<br />
True mastery never seeks to belittle, or boast, or distress. True mastery contains the real humility this false master has accused me of lacking.<br />
<br />
True mastery comes from love.<br />
<br />
The person concerned, a prominent voice in the "SharePoint community", is known for, and prides himself on, his intolerance. I'm sure I'm not the first to earn his displeasure, and no doubt his response to me would be along the same tired lines of oh I can't take criticism, I write bad code and I won't learn.<br />
<br />
To which I would respond: Strive to be fit to <b>teach </b>first.susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-48396338417551554472013-01-23T15:25:00.001-08:002013-01-25T00:53:06.496-08:00Customising the NewForm.aspx of a List using Server Side CodeEdit - I am getting some feedback from other devs the sum of which is "forget this and do it via jQuery instead". It is worth noting that this is NOT the recommended way of customising a NewForm or EditForm aspx page, so proceed at your own risk. JQuery is downloadable for free at jquery.com. When I figure out how to use it I will post a follow up!<br />
<br />
I've been meaning to write this for ages. The first thing I should mention is that you're not meant to do this. Believe me I have fought the Sharepoint markup generator every step of the way on this one. It wants you to use JQuery and the ECMA client script. But god JQuery is a pain and I don't know it. And it's hard to read or pick up, and dammit I want to use C flippin sharp and nothing and no-one is going to stop me.<br />
<br />
OK. As we say in Ireland when someone asks for directions: "If I were you, I wouldn't start from here." But if you DO start from here...<br />
<br />
Firstly, you aren't allowed to run code on aspx pages that run in SharePoint. So you have to go into the web config, go to the <PageParserPaths> section and enter something like the following:<br />
<span style="background-color: #f1f1f1; font-family: Monaco, 'Lucida Console', monospace; font-size: 13px; line-height: 18.1875px; white-space: nowrap;"><PageParserPath VirtualPath="http://myserver/mySiteColl/MyList/MyForm.aspx" CompilationMode="Always" AllowServerSideScript="true" /></span><br />
<span style="background-color: #f1f1f1; font-family: Monaco, 'Lucida Console', monospace; font-size: 13px; line-height: 18.1875px; white-space: nowrap;"><br /></span>
<br />
<a href="http://blog.pixelmill.com/1037/sharepoint-2010-allow-server-side-code-inline-code-blocks/" target="_blank">More info on Page Parser Paths here</a>...<br />
<br />
OK run an iisreset and that will allow you to customise the NewForm.aspx. Microsoft do not recommend fiddling with the form itself as if you break it, you break the list. So fire up SharePoint Designer to create a new page.<br />
<br />
<a href="http://sharepoint07.wordpress.com/2008/02/05/customize-the-newformaspx/" target="_blank">More info on customising the page here</a><br />
<br />
Now I'm aware that there's this nifty plugin called InfoPath which will do the forms for you. Do we have that in my workplace? Not a chance. Not on the cards until we upgrade. So just in case you ask me "what about infopath", that's the answer.<br />
<br />
Right. I do this in SharePoint designer. Now that the MyForm.aspx has been created, click the "Code" tab. But before we continue it is important to note the following:<br />
<br />
<b>None of the standard SharePoint OK or Cancel buttons is customisable.</b><br />
<b><br /></b>
Trust me. I tried. Your best bet is hiding and creating a custom HTML command button of your own. Then you can have runat=server, onclick = "DoTheWork" etc. Then, of course, you have to create a script to do the work :)<br />
<br />
So, right up the top of your HTML, put up the following. Note that<br />
<br />
(1) when getting the list name, I am using the request parameter and ascertaining the position of the string "MyForm". This means I can re-use the code in other lists if I want.<br />
<br />
(2) The ContentPlaceHolder bit of code is how I get to "talk" to the sharepoint form controls through the code. Trying to figure out where the controls are and what they're called and how to get the damn data out of them is like breaking into Fort Knox. So if I want to save back to the list (remember, there is no way to get to the save routine via the OK button BECAUSE I HAVE TRIED)<br />
<br />
I need to go right into the markup, grab the guid of the data form web part and then furthermore pull out the name from the the properties tab on the left in SP Designer (it's always ff_number) and then get the value. And don't talk to me about radio buttons. I can't get it to work with radio buttons.<br />
<br />
I've done a uservalue control because it's the trickiest.<br />
<br />
(3) The ID at the end of the request string will always be the ID on the list item you're looking for, so I used that to retrieve the item.<br />
<br />
(4) All saving of items is done through the SPListItem object and the Update() method<br />
<br />
(5) I don't have a sharepoint environment and compiler with me right now, so chances are there are a couple of errors in the script, but nothing major. Code below the jump - enjoy!<br />
<br />
<br />
<a name='more'></a><br />
<br />
<br />
<br />
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace;"><span style="font-size: x-small; line-height: 1.2em; outline: none;"> </span><span style="font-size: x-small; line-height: 1.2em;"><script type="text/c#" runat="server"></span></span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> </span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> protected void SendMailToUser(object sender, EventArgs e)</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> {</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> string cSiteName = "<span style="color: blue; line-height: 1.2em; outline: none;"><u style="line-height: 1.2em; outline: none;"><a href="http://connect/grp/it/" rel="nofollow" style="color: #003399; line-height: 1.2em; outline: none;" target="_blank">http://</a>myserver/mysitecoll/</u></span>";</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> string cListName = string.Empty;</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> SPSite site = new SPSite(cSiteName);</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> SPWeb web = site.OpenWeb();</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> //which list is it?</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> int start = 0;</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> int end = 0;</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> string requestUrl = Request.Url.ToString();</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> start = requestUrl.IndexOf("Lists");</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> start = start + 6;</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> end = requestUrl.IndexOf("/MyForm");</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> cListName = requestUrl.Substring(start, end - start);</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> </span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> //retrieve list item</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> try</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> {</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> ContentPlaceHolder holder = (ContentPlaceHolder)Master.FindControl("PlaceHolderMain");</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> DataFormWebPart dwp = (DataFormWebPart)holder.FindControl("c2a69f84-b81f-4cd6-a952-4b27add4c177");</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> FormField userCtrl = (FormField)dwp.FindControl("ff8_1");</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> string userValue = userCtrl.Value.ToString();</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> int pos = userValue.IndexOf("#");</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> userValue = userValue.Substring(pos + 1); </span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> //get request param id</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> string param=Server.UrlDecode(Request.QueryString["ID"]).ToString();</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> int itemID = Int32.Parse(param);</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> SPList list = web.Lists[cListName];</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> SPListItem item = list.GetItemById(itemID);</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> item.Update();</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> //get user</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> string userValue1 = item["User Field"].ToString(); </span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> SPFieldUserValue fieldUserValue = new SPFieldUserValue(web,userValue1); </span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> SPUser user = fieldUserValue.User;</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> string requesterEmail = user.Email;</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> </span><span style="font-family: 'Courier New', Courier, monospace; font-size: x-small; line-height: 1.2em;"> </span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> //send email IT Support</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> string body = string.Empty;</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> string subject = string.Empty;</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> body = "Dear " + user.Name + " this is a link to <br/><br/> <a href='http://connect/grp/it/Lists/" + list.Title.Replace(" ", "%20") + "/MyForm.aspx?ID=" + itemID.ToString() + "'>Click the following link</a>";</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> subject = "Approval Required for New Request";</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> errorArgs = "line 6";</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> SPUtility.SendEmail(web, false , false, requesterEmail, subject, body, false);</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> //confirm done</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> string done = "<script language='javascript'>alert('Email sent to user'" + user.Name + ");</scrirt>";</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> done = done.Replace("scrirt","script");</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> Response.Write(done);</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> }</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> </span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> catch (Exception ex)</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> {</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> Response.Write(ex.Message); </span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> }</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> finally</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> {</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> web.Dispose();</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> site.Dispose();</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> Response.Redirect(cSiteName + "/Lists/" + cListName + "/MyItems.aspx");</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> } </span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"> }</span></div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small; line-height: 1.2em; outline: none;"></script></span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19.1875px; margin: 0px; outline: none; padding: 0px;">
</div>
<br />
<br />susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-6149849737304800012012-10-19T10:59:00.003-07:002012-10-19T10:59:42.971-07:00I Deleted My Last Post About Dynamically Rendering Controls From a SharePoint Listbecause I was stupid enough to forget that it is impossible, without a lot of fuss and bother, to add event handing to controls which are only rendered at runtime. I'll just take my dunce hat and sit in the corner.<br />
<br />
It was not a total waste of time, however, as I was able to use the looping code and Response.Write to write the markup to a text file and then stick it back into the ASPX page. But as they say when looking for directions in Ireland, my home country, "well I wouldn't start from here if I were you."<br />
<br />
Sigh. The loop DOES work well when submitting to list and that's coming up shortly.susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-77133751618107503682012-10-01T16:33:00.000-07:002012-10-01T16:33:28.980-07:00Oi! My Content Editor Web Part is Borked!Had an issue today where I got an email saying "ever had this error before?" It was a Content Editor Web Part (those thingies that allow you to add fancy text to a SharePoint page via a web part) that was causing the browser to throw errors. The browser was apparently failing to recognise the background colour or some other bit of javascript. Also the context menu that dropped down from the "edit" command on the web part - viewable when you select "Edit Page" - was not dropping down. The whole thing seemed borked.<br />
<br />
I tried adding in a few other Content Editor Web Parts and for the most part they broke in the same way. I closed them using the Web Parts Maintenance Page (which is like the regular page only you add ?contents=1 to the URL) and then had a look at the original web part. Clicking Source Text Editor, I noticed the following.<br />
<DIV blah blah blah piles of attributes that looked as if they'd been lifted out of MS Word etc etc><br />
<p>Text</p><br />
</DIV><br />
<br />
Since the DIV contained no text or formatting, I deleted it and refreshed the web part. Hey presto, the borkedness was fixed. The tags were interfering with the HTML. Interesting indeed if this is being caused by SharePoint's very own Rich Text Editor!susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-89566154498214920932012-09-27T14:47:00.000-07:002012-09-27T14:47:17.143-07:00Help! I can't create a Site Collection without getting "Access Denied" errors!Another one of those problems which is a short post but which took me hours upon hours to fix. The error is as follows:<br />
<br />
You log into SharePoint Central Admin with the intent of creating a site collection. You go to Applications and do the necessary, ensuring it is added to the correct Web Application yada yada yada. And then you go to the new link for your site collection - because you want to create some sites in it, naturally enough.<br />
<br />
Access Denied Error!<br />
<br />
Butbutbut - I'm the Farm Administrator! How can this be? How can I not see my own damn site collections?<br />
<br />
By the time I had found the solution, via the good offices of google, I was quite ready to be the Funny Farm Administrator. But never mind. I know now what it is.<br />
<br />
<br />
1. Central Admin. Attempt to access SharedServices1 page. Chances are you will get an Access Denied error<br />
<br />
2. Application Management - Policy for Web Application. Is your farm account listed there with Full Control? If not, add it for the relevant Web Application (you'll see the web app filter in the view dropdown on the top right)<br />
<br />
3. Still having problem? Go to Operations - Service Accounts. BE VERY CAREFUL ABOUT DOING THIS ON A PRODUCTION SERVER, HAVE A BACKUP FIRST:<br />
Click "Web application pool" option button<br />
Select Web Service - Winds SharePoint Services Web Application<br />
Select Application Pool - usually only Sharepoint - 80 available. That's fine.<br />
Select "Configurable" radio button and enter farm admin account user and password<br />
Click OK<br />
A lot of the links in SharedServices will still be broken but you will be able to view SharedServices1 and more importantly your site collections!<br />
<br />
susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-63976581843123732182012-08-13T05:30:00.004-07:002012-08-13T05:33:06.386-07:00Impersonating a SharePoint searchHere is code which will run a SharePoint search and return the results in a StreamWriter object, which can be outputted to a file. Hook it up to a console app and off you go!<br />
<div>
Note </div>
<div>
- you must specify a row limit or it will only return the first 50 results</div>
<div>
- user under which it runs will need to have read access to the content database containing the Central Administration Shared Services. This is usually called SharedServices1_DB or some such variant. Error logs will indicate very clearly if that error occurs so make sure to have good error handling</div>
<div>
- Microsoft.Office.Server.Search.Query is another one of those annoying DLLs that is impossible to find. It's not one of the SharePoint dlls - you might find it in the ISAPI folder or otherwise in the list of other .NET dlls.</div>
<div>
<br /></div>
<div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">using<span style="color: black; line-height: 1.2em; outline: none;"> System.IO;</span></span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">using<span style="color: black; line-height: 1.2em; outline: none;"> Microsoft.SharePoint;</span></span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">using<span style="color: black; line-height: 1.2em; outline: none;"> Microsoft.SharePoint.Utilities;</span></span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">using<span style="color: black; line-height: 1.2em; outline: none;"> Microsoft.Office.Server;</span></span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">using<span style="color: black; line-height: 1.2em; outline: none;"> Microsoft.Office.Server.Search;</span></span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">public<span style="color: black; line-height: 1.2em; outline: none;"> </span>class<span style="color: black; line-height: 1.2em; outline: none;"> </span><span style="color: #2b91af; line-height: 1.2em; outline: none;">SearchResults</span></span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">public</span> <span style="color: blue; line-height: 1.2em; outline: none;">string</span> errorArgs;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">public</span> SearchResults()</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: green; line-height: 1.2em; outline: none;">//constructor don't do nuthin'</span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
errorArgs = <span style="color: blue; line-height: 1.2em; outline: none;">string</span>.Empty;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">protected</span> <span style="color: #2b91af; line-height: 1.2em; outline: none;">DataTable</span> ReturnSearchResults(<span style="color: blue; line-height: 1.2em; outline: none;">string</span> searchCriterion, <span style="color: blue; line-height: 1.2em; outline: none;">string</span> siteUrl)</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: #2b91af; line-height: 1.2em; outline: none;">DataTable</span> dt = <span style="color: blue; line-height: 1.2em; outline: none;">new</span> <span style="color: #2b91af; line-height: 1.2em; outline: none;">DataTable</span>();</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">try</span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: green; line-height: 1.2em; outline: none;">//Available fields: Title,FileExtension,ContentType,Created,LastModifiedTime,IsDocument,owsStartDate,Path</span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
searchCriterion = searchCriterion.Replace(<span style="color: #a31515; line-height: 1.2em; outline: none;">" "</span>, <span style="color: #a31515; line-height: 1.2em; outline: none;">"+"</span>);</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">string</span> fullTextSQL = <span style="color: #a31515; line-height: 1.2em; outline: none;">"SELECT Title, FileExtension, ContentClass, ContentType, Created, URL FROM SCOPE() "</span>;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
fullTextSQL = fullTextSQL + <span style="color: #a31515; line-height: 1.2em; outline: none;">"WHERE CONTAINS ('"</span> + searchCriterion + <span style="color: #a31515; line-height: 1.2em; outline: none;">"') "</span>;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: green; line-height: 1.2em; outline: none;">//do whole site for the moment</span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">using</span> (<span style="color: #2b91af; line-height: 1.2em; outline: none;">SPSite</span> site = <span style="color: blue; line-height: 1.2em; outline: none;">new</span> <span style="color: #2b91af; line-height: 1.2em; outline: none;">SPSite</span>(siteUrl))</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
errorArgs = <span style="color: #a31515; line-height: 1.2em; outline: none;">"accessing office search component"</span>;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
Microsoft.Office.Server.Search.Query.<span style="color: #2b91af; line-height: 1.2em; outline: none;">FullTextSqlQuery</span> search = <span style="color: blue; line-height: 1.2em; outline: none;">new</span>Microsoft.Office.Server.Search.Query.<span style="color: #2b91af; line-height: 1.2em; outline: none;">FullTextSqlQuery</span>(site);</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
Microsoft.Office.Server.Search.Query.<span style="color: #2b91af; line-height: 1.2em; outline: none;">ResultTableCollection</span> results;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: green; line-height: 1.2em; outline: none;">//configure the query - limit it to 50 for the moment</span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
errorArgs = <span style="color: #a31515; line-height: 1.2em; outline: none;">"preparing search"</span>;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
search.QueryText = fullTextSQL;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
search.ResultTypes = Microsoft.Office.Server.Search.Query.<span style="color: #2b91af; line-height: 1.2em; outline: none;">ResultType</span>.RelevantResults;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
search.RowLimit = 1000;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
search.TrimDuplicates = <span style="color: blue; line-height: 1.2em; outline: none;">true</span>;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
results = search.Execute();</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
errorArgs = <span style="color: #a31515; line-height: 1.2em; outline: none;">"search successful"</span>;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">if</span> (results.Count > 0)</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">using</span> (Microsoft.Office.Server.Search.Query.<span style="color: #2b91af; line-height: 1.2em; outline: none;">ResultTable</span> relevantResults = results[Microsoft.Office.Server.Search.Query.<span style="color: #2b91af; line-height: 1.2em; outline: none;">ResultType</span>.RelevantResults])</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{ dt.Load(relevantResults, <span style="color: #2b91af; line-height: 1.2em; outline: none;">LoadOption</span>.OverwriteChanges); }</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">return</span> dt;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">catch</span> (<span style="color: #2b91af; line-height: 1.2em; outline: none;">Exception</span> ex)</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
//handle error here</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">return</span> <span style="color: blue; line-height: 1.2em; outline: none;">null</span>;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">finally</span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">public</span> <span style="color: #2b91af; line-height: 1.2em; outline: none;">StreamWriter</span> ReturnResultsInStream(<span style="color: blue; line-height: 1.2em; outline: none;">string</span> searchCriterion, <span style="color: blue; line-height: 1.2em; outline: none;">string</span> siteUrl,<span style="color: #2b91af; line-height: 1.2em; outline: none;">FileStream</span> fs)</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">try</span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: #2b91af; line-height: 1.2em; outline: none;">StreamWriter</span> stream = <span style="color: blue; line-height: 1.2em; outline: none;">new</span> <span style="color: #2b91af; line-height: 1.2em; outline: none;">StreamWriter</span>(fs);</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: #2b91af; line-height: 1.2em; outline: none;">DataTable</span> results = <span style="color: blue; line-height: 1.2em; outline: none;">this</span>.ReturnSearchResults(searchCriterion, siteUrl);</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">foreach</span> (<span style="color: #2b91af; line-height: 1.2em; outline: none;">DataRow</span> dr <span style="color: blue; line-height: 1.2em; outline: none;">in</span> results.Rows)</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">foreach</span> (<span style="color: blue; line-height: 1.2em; outline: none;">object</span> field <span style="color: blue; line-height: 1.2em; outline: none;">in</span> dr.ItemArray)</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">if</span> (field <span style="color: blue; line-height: 1.2em; outline: none;">is</span> <span style="color: blue; line-height: 1.2em; outline: none;">string</span>)</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
stream.Write(field.ToString());</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
stream.Write(<span style="color: #a31515; line-height: 1.2em; outline: none;">";"</span>);</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
stream.WriteLine();</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">return</span> stream;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">catch</span>(<span style="color: #2b91af; line-height: 1.2em; outline: none;">Exception</span> ex)</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: green; line-height: 1.2em; outline: none;">//HANDLE YOUR ERROR HERE</span></div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
<span style="color: blue; line-height: 1.2em; outline: none;">return</span> <span style="color: blue; line-height: 1.2em; outline: none;">null</span>;</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15.600000381469727px; margin: 0px; outline: none; padding: 0px;">
</div>
</div>
susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-50364606792858288352012-06-16T04:38:00.004-07:002012-06-16T04:40:11.335-07:00An Addendum to the Last Post: Only Works on Document LibrariesI should have noted this at the time, but I was not aware of it. It was only when I tried to do the same thing with an SPList object that I ran into trouble.<br />
<br />
<a href="http://www.synergyonline.com/Blog/Lists/Posts/Post.aspx?ID=122" target="_blank">This is why - BeforeProperties and AfterProperties do not work on a list</a><br />
<br />
So I ended up having to create a Just Updated flag for the user to click when they want to send an email. Just before ending the event handler, it sets that flag back to false. Yet another hack to make up for SharePoint 2007's "by design behaviour" (sigh)<br />
<br />
So if using the code in the previous post - document library only, lads. Sorry.susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-57857700340461776672012-05-19T08:20:00.004-07:002012-05-19T08:23:49.687-07:00How to use BeforeProperties and AfterProperties in an RunWithElevatedPrivileges blockI came up against a tricky problem this week when I wrote code that would not work for a non-admin account and needed to wrap it up in a privilege block (note: I am not using RunWithElevatedPrivileges as it's dodgy, see earlier blog entries on the subject, but it's along similar lines.)<br />
<div>
<br /></div>
<div>
As explained before, an override method for an <span style="font-family: 'Courier New', Courier, monospace;">SPItemEventReceiver </span><span style="font-family: inherit;">object always passes in the </span><span style="font-family: 'Courier New', Courier, monospace;">properties </span><span style="font-family: inherit;">of the list item as a parameter. This allows for manipulation of the object. </span></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div>
<span style="font-family: inherit;">But we also know that the properties object cannot be used within the </span><span style="font-family: 'Courier New', Courier, monospace;">SPSecurity.RunWithElevatedPrivileges </span><span style="font-family: inherit;">block as an entirely new site object needs to be created with separate web, list and item objects created with it. This is fine usually, but what if we need to compare the value of a column before being updated with the results after it has been changed? Usually we would be able to avail of </span><span style="font-family: 'Courier New', Courier, monospace;">properties.BeforeProperties["Fieldname"]</span><span style="font-family: inherit;"> and </span><span style="font-family: 'Courier New', Courier, monospace;">properties.AfterProperties["Fieldname"]</span><span style="font-family: inherit;"> and compare the two, but our new </span><span style="font-family: 'Courier New', Courier, monospace;">SPListItem</span><span style="font-family: inherit;"> object inside the privilege block does not possess either of these attributes. So, what to do?</span></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div>
<span style="font-family: inherit;">The solution is below - important lines in bold:</span></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="color: #2b91af; line-height: 1.2em; outline: none;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"><b style="line-height: 1.2em; outline: none;">SPItemEventDataCollection</b></span><span style="color: black; font-family: 'Courier New'; line-height: 1.2em; outline: none;"><b style="line-height: 1.2em; outline: none;"> before = properties.BeforeProperties;</b></span></span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"><span style="color: #2b91af; line-height: 1.2em; outline: none;"><b style="line-height: 1.2em; outline: none;">SPItemEventDataCollection</b></span><b style="line-height: 1.2em; outline: none;"> after = properties.AfterProperties;</b></span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="color: #2b91af; font-family: 'Courier New'; line-height: 1.2em; outline: none;">Guid<span style="color: black; line-height: 1.2em; outline: none;"> siteID = properties.SiteId;</span></span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="color: #2b91af; font-family: 'Courier New'; line-height: 1.2em; outline: none;">Guid<span style="color: black; line-height: 1.2em; outline: none;"> webID = properties.ListItem.Web.ID; </span></span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"><span style="color: green; line-height: 1.2em; outline: none;">//this is for getting the user Token to log in as system</span></span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"><span style="color: #2b91af; line-height: 1.2em; outline: none;">SPSite</span> tempSite = <span style="color: blue; line-height: 1.2em; outline: none;">new</span> <span style="color: #2b91af; line-height: 1.2em; outline: none;">SPSite</span>(siteID);</span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"><span style="color: #2b91af; line-height: 1.2em; outline: none;">SPUserToken</span> sysToken = tempSite.SystemAccount.UserToken;</span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"><span style="color: green; line-height: 1.2em; outline: none;">//need to dump this as not declared in a using block</span></span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;">tempSite.Dispose();</span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"><span style="color: green; line-height: 1.2em; outline: none;">//start the privilege block</span></span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"><span style="color: blue; line-height: 1.2em; outline: none;">using</span> (<span style="color: #2b91af; line-height: 1.2em; outline: none;">SPSite</span> site = <span style="color: blue; line-height: 1.2em; outline: none;">new</span> <span style="color: #2b91af; line-height: 1.2em; outline: none;">SPSite</span>(siteID, sysToken))</span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;">{</span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"> <span style="color: blue; line-height: 1.2em; outline: none;">using</span> (<span style="color: #2b91af; line-height: 1.2em; outline: none;">SPWeb</span> web = site.OpenWeb(webID))</span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"> {</span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"> <span style="color: blue; line-height: 1.2em; outline: none;"><b style="line-height: 1.2em; outline: none;">if</b></span><b style="line-height: 1.2em; outline: none;"> (before[</b><span style="color: #a31515; line-height: 1.2em; outline: none;"><b style="line-height: 1.2em; outline: none;">"FieldName"</b></span><b style="line-height: 1.2em; outline: none;">].ToString() != after[</b><span style="color: #a31515; line-height: 1.2em; outline: none;"><b style="line-height: 1.2em; outline: none;">"FieldName"</b></span><b style="line-height: 1.2em; outline: none;">].ToString())</b></span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"> { someBooleanVariable = <span style="color: blue; line-height: 1.2em; outline: none;">true</span>; }</span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="color: green; font-family: 'Courier New'; line-height: 1.2em; outline: none;"> //do work</span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
</div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em; outline: none;"> }</span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em;"><br /></span></div>
<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin: 0px; outline: none; padding: 0px;">
<span style="font-family: 'Courier New'; line-height: 1.2em;">}</span></div>
</div>susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-34295220851741164682012-04-26T10:33:00.001-07:002012-04-26T10:35:32.449-07:00Argh! I Locked Myself Out!But I didn't get back in by being given a key - more like kicking the door down, but anyway...<br />
<br />
A few days ago I was testing something on the dev machine and removed a few user groups from the site permissions list on a site collection. They were still associated with the site, just had no site permissions. I was logged in as site admin. Went to do something on one of those sites and what do you know, I can't get in! Checked the site collection admin, where I was queen of everything and all looked well. Checked Central Admin and the Sharepoint:80 web application, found my errant site collection, and yes I was site admin. Still couldn't get in.<br />
<br />
Here is how I got back in. I do not recommend using this as a method to get in. It is a <b>horrible hack</b> and will consign me to SharePoint Hell. I only did it because it was a dev machine and the outcome was not important. However - it<b> is </b>an example of how to dynamically add an existing SharePoint user group onto a Site Permissions list if it isn't on it already:<br />
<br />
<br />
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">using<span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> (</span><span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPSite</span><span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> site = </span>new<span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> </span><span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPSite</span><span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">(</span><span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"<a href="http://notmyemployerssitecoll/sc/sample/" rel="nofollow" style="color: #003399; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" target="_blank">http://notmyemployerssitecoll/sc/sample/</a>"</span><span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">))</span></span></div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">using</span> (<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPWeb</span> web = site.OpenWeb())</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">foreach</span> (<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPGroup</span> group <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">in</span> web.AssociatedGroups)</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">if</span> (group.Name.Contains(<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"My Little Lost Group"</span>))</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
{</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<span style="color: green; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">//assign it permissions to control everything</span></div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<span style="color: green; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">//obviously when we do this for real, we want it to grab the</span></div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<span style="color: green; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">//existing role assignment</span></div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPRoleAssignment</span> roleAssignment = <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">new</span><span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPRoleAssignment</span>(web.SiteGroups[group.Name]);</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPRoleDefinitionBindingCollection</span> roleDefinition = roleAssignment.RoleDefinitionBindings;</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
roleDefinition.Add(web.RoleDefinitions[<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"Full Control"</span>]);</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
web.RoleAssignments.Add(roleAssignment);</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
web.Properties[group.Name] = <span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"Full Control"</span>;</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
web.Properties.Update();</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
}</div>
<div style="background-color: white; font-family: 'Courier New', monospace; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">
}</div>susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-32949491210855747392012-04-18T14:07:00.000-07:002012-04-18T14:07:52.951-07:00SPUtility.RunWithElevatedPrivileges Fails with getting SPUser from groups<div style="background-color: white; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Using SPUtility.RunWithElevatedPrivileges fails when trying to get SPUser objects from groups. It worked when I removed the privilege block, but this was not practical for non-admin users. The code below worked perfectly for me:</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><br />
</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><br />
</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Guid</span> groupWebID = <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPContext</span>.Current.Web.ID;</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Guid</span> groupSiteID = <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPContext</span>.Current.Site.ID;</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPUserToken</span> sysToken = <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPContext</span>.Current.Site.SystemAccount.UserToken;</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> </div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">using</span> (<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPSite</span> groupSite = <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">new</span> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPSite</span>(groupSiteID, sysToken))</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> {</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">using</span> (<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPWeb</span> groupWeb = groupSite.OpenWeb(groupWebID))</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> {</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> </div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPGroup</span> group = groupWeb.Groups[<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"My Group"</span>];</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">foreach</span> (<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPUser</span> user <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">in</span> group.Users)</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> {</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> AllUsers.Items.Add(user.Email);</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> }</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> </div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> }</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> }</div>susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-60129747896286217012012-04-15T09:19:00.001-07:002012-04-15T09:20:39.708-07:00Not SP-Related: Searching for a Proper Name Within Range of a WordThis is not related to the day job or to sharepoint but it is coding stuff so here is as good a place as any to put it. I rarely do geek stuff in my free time but I thought this might be of some interest.<br />
<br />
I was having a discussion with a very nice journalist on the tweet machine a few weeks ago and proposed I write some code for her. She wanted to search a Very Large CSV file (which I won't name cos it's a bit political and this blog is not about politics :-) about 2GB in size for names surrounding various words. One of these words was the word "whistleblower".<br />
<br />
I came up with an algorithm that would search either a 50-character radius either side of the instance of the word, or, if there weren't enough characters, the whole line (I was asking the filestream to ReadLine() each time) It would look for a word that had a capital letter that was not preceded by a space and a full stop. In order to make it run, I downloaded the freebie edition of Microsoft Visual Studio C# Express, which had everything I needed for the purpose.<br />
<br />
Now this is not perfect and it needs quite a bit of work. For a start, I need to write some extra code to grab the initial code of each cable (yeah, it's becoming more obvious what I'm referring to here, for the record allow me to say that I have no interest in the cause this organisation espouses, or affection for its leaders, but given it is now in the public domain...::shrugs::) But if I wait till I do that, I might never get it done. Also the file has the annoying habit of containing a lot of acronyms and randomly capitalised words, a prob which I can't get around at present.<br />
<br />
<b>What this code does:</b> Examines file and searches for every instance of the word "whistleblower" (word can be changed) then writes everything to a file called results.txt, which will be found in the \bin folder of your C# project.<br />
<br />
<b>What you need to do.</b> Download VS, Create a console application, go to Program.cs and copy this in wholesale. Sorry about the brackets, it should format them properly for you once it's pasted in<br />
<br />
Oh and you need to download the source file. There are plenty of web resources telling you how to do that :)<br />
<br />
<br />
<div class="western" style="margin-bottom: 0cm;"><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">using</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> System;</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">using</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> System.Collections.Generic;</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">using</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> System.Text;</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">using</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> System.IO;</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">using</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> System.Reflection;</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">namespace</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> ReadFromFile</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"><span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">class</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> </span></span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">Program</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">static</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> </span></span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">void</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> Main(</span></span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">string</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">[] args)</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">StreamReader</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> reader = </span></span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">null</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">;</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: green;"><span style="font-family: Consolas, sans-serif;">//write to file - change YourFolder to your actual folder!</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">FileStream</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> fs = </span></span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">new</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> </span></span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">FileStream</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">(</span></span><span style="color: #a31515;"><span style="font-family: Consolas, sans-serif;">"C:\\YourFolder\\results.txt"</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">, </span></span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">FileMode</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">.Create);</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">StreamWriter</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> sw = </span></span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">new</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> </span></span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">StreamWriter</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">(fs);</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">TextWriter</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> tmp = </span></span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">Console</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">.Out;</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">try</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span> </div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: green;"><span style="font-family: Consolas, sans-serif;">//read from file </span></span><span style="color: green; font-family: Consolas, sans-serif;">- change YourFolder to your actual folder!</span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">string</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> fileName = </span></span><span style="color: #a31515;"><span style="font-family: Consolas, sans-serif;">"C:\\YourFolder\\cables.csv"</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">;</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">reader = </span></span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">File</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">.OpenText(fileName);</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">Console</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">.SetOut(sw);</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">while</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> (!reader.EndOfStream)</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">string</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> line = reader.ReadLine();</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">int</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> position = -1;</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"><span style="font-family: Consolas, sans-serif;">//change to lower case version of word</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">position = line.IndexOf(</span></span><span style="color: #a31515;"><span style="font-family: Consolas, sans-serif;">"whistleblower"</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">);</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">if</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> (position == -1)</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"><span style="font-family: Consolas, sans-serif;">//change to capitalised version of word</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">position = line.IndexOf(</span></span><span style="color: #a31515;"><span style="font-family: Consolas, sans-serif;">"Whistleblower"</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">);</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">if</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> (position == -1)</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: green;"><span style="font-family: Consolas, sans-serif;">//no instances, next row</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">continue</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">;</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: green;"><span style="font-family: Consolas, sans-serif;">//we have a value for required string</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">string</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> subLine = line;</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">if</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> ((position - 50) >= 0 && ((position + 50) <= line.Length))</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">subLine = line.Substring(position - 50, position + 50);</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">else</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">subLine = line.Substring(0, line.Length);</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">char</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">[] characters = line.ToCharArray();</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">for</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> (</span></span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">int</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> i = 0; i < characters.Length; i++)</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">if</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> (</span></span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">Char</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">.IsLetter(characters[i]) && (i >= 2))</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">if</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> ((!characters[i - 2].Equals(</span></span><span style="color: #a31515;"><span style="font-family: Consolas, sans-serif;">"."</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">)))</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">if</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> (</span></span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">Char</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">.IsUpper(characters[i]))</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{ </span></span><span style="color: green;"><span style="font-family: Consolas, sans-serif;">//do stuff</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: green;"><span style="font-family: Consolas, sans-serif;">//start from i and go to next space</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">Console</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">.Write(</span></span><span style="color: #a31515;"><span style="font-family: Consolas, sans-serif;">"Found possible name string: "</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">);</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">for</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> (</span></span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">int</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> j = i; j < characters.Length; j++)</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">if</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;"> (!</span></span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">Char</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">.IsWhiteSpace(characters[j]))</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{ </span></span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">Console</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">.Write(characters[j]); }</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">else</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{ </span></span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">Console</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">.WriteLine(); </span></span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">break</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">; }</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: blue;"><span style="font-family: Consolas, sans-serif;">finally</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">{</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: green;"><span style="font-family: Consolas, sans-serif;">//close in and out files</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">sw.Close();</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">fs.Close();</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">reader.Close();</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: green;"><span style="font-family: Consolas, sans-serif;">//reset console output</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> </span><span style="color: #2b91af;"><span style="font-family: Consolas, sans-serif;">Console</span></span><span style="color: black;"><span style="font-family: Consolas, sans-serif;">.SetOut(tmp);</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"> <span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><span style="color: black;"><span style="font-family: Consolas, sans-serif;">}</span></span></div><div class="western" style="margin-bottom: 0cm;"><br />
</div><div class="western" style="margin-bottom: 0cm;"><br />
</div>susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-48436615052579008552012-04-11T05:17:00.001-07:002012-04-11T05:19:21.250-07:00Deleting Items on a List using SPWeb.ProcessBatchData()I had a situation a week ago where I needed to write a script to delete all items in a list while maintaining the list structure. Now for those familiar with the SharePoint API, the obvious solution would be something like this:<br />
<br />
<span style="font-family: 'Courier New', Courier, monospace;">foreach (SPListItem item in list.Items)</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">{</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"> item.Delete();</span><br />
<span style="font-family: 'Courier New', Courier, monospace;">}</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br />
</span><br />
<span style="font-family: inherit;">However the</span><span style="font-family: 'Courier New', Courier, monospace;">IEnumerator</span><span style="font-family: inherit;"> interface does not like deleting things while iterating through them because the list count will change while you're doing it. This is mighty confusing for an iterating loop so that's handled as an error and SharePoint sez, no can do, sorry. I tried using list.Items[0].Delete() as I thought that might work since there will always be at least one item in the collection, but no - when it got to the end of the collection it threw an error and all my old VB programming days of On Error Resume Next could not help me in my attempts to keep the code running after the error.</span><br />
<span style="font-family: inherit;"><br />
</span><br />
<span style="font-family: inherit;">But anyway. All this has happily become a moot point since discovering the </span><span style="font-family: 'Courier New', Courier, monospace;">SPWeb.ProcessBatchData()</span><span style="font-family: inherit;"> command, since all I have to do is loop through every list in the web and issue a delete command. At first I got to the Microsoft help for this (always, always a bad idea. Sorry, Microsoft, but there it is) and got scared off by talk of OWS files and piles of XML. How and ever, after a bit of digging I found a great entry on Stack Overflow that assures me one does not need to be rooting around obscure XML files in the Twelve Hive in ungraceful fashion, but can build the XML on the fly as a string, feed it in to the ProcessBatchData command and hey presto, you're off:</span><br />
<span style="font-family: inherit;"><br />
</span><br />
<br />
<div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">foreach</span> (<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPList</span> list <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">in</span> web.Lists)</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> {</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">try</span></div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> {</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: green; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">//decrement loop so as not to confuse the iEnumerator interface</span></div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Console</span>.WriteLine(<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"Deleting list items in following list : "</span> + list.Title);</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPListItemCollection</span> splic = list.Items;</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">StringBuilder</span> batchString = <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">new</span> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">StringBuilder</span>();</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> batchString.Append(<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>"</span>);</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">foreach</span> (<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPListItem</span> item <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">in</span> splic)</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> {</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> batchString.Append(<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"<Method>"</span>);</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> batchString.Append(<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"<SetList Scope=\"Request\">"</span> + <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Convert</span>.ToString(item.ParentList.ID) + <span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"</SetList>"</span>);</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> batchString.Append(<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"<SetVar Name=\"ID\">"</span> + <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Convert</span>.ToString(item.ID) + <span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"</SetVar>"</span>);</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> batchString.Append(<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"<SetVar Name=\"Cmd\">Delete</SetVar>"</span>);</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> batchString.Append(<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"</Method>"</span>);</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> }</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> batchString.Append(<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"</Batch>"</span>);</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> web.ProcessBatchData(batchString.ToString());</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> }</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">catch</span> { //whatever }</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><br />
</div><div style="background-color: white; font-family: 'Courier New'; font-size: small; line-height: 15px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"> }</div><br />
<span style="font-family: inherit;"><br />
</span><br />
<span style="font-family: inherit;"><br />
</span><br />
Please note that I actually found this code somewhere on Stack Overflow, but have gone and lost the link. So Unknown Programmer, you have my greatest gratitude for this!<br />
<br />
I believe there are Copy and Insert methods also but will have to check those out. I may have a requirement for these, due to SharePoint UI inflexibility, so stay tuned...susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com11tag:blogger.com,1999:blog-7589311256418017787.post-76919988918695675742012-02-13T16:54:00.000-08:002012-02-13T16:55:57.615-08:00Retrieving MySite Info to a list via a User Value Field<div><span style="font-family: Verdana, sans-serif;">Greetings. I have not updated this blog from some time because I was on leave all of January and was relaxing and enjoying myself. I follow SharePoint on my twitter feed and one of the tweets was "Do you dream about #SharePoint?" and I RT'ed the tweet with the additional comment, "You've got to be joking!" But now I am back and thoughts of such things return to my mind. This will be my first post for 2012, then. I spent quite some time struggling with this one. As far as I know it's the one place where the whole routine is present in one place so if anyone needs it they can use it.</span></div><div><span style="font-family: Verdana, sans-serif;"><br />
</span></div><span style="font-family: Verdana, sans-serif;">If you want to get more info on a user, often you'll click on their MySite. This code effectively does the same thing. If you have a list or library that contains a "user or group" control, you can retrieve the user information from the field and you can use the UserProfileManager object to get all the info on that user. To do this, you're going to need some libraries that you might not normally have in your .NET store. So, first:</span><br />
<div><br />
</div><div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="color: blue; font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">using<span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> System;</span></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="color: blue; font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">using<span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> Microsoft.SharePoint;</span></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="color: blue; font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">using<span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> Microsoft.Office.Server;</span></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="color: blue; font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">using<span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> Microsoft.Office.Server.UserProfiles;</span></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="color: blue; font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">using<span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> System.Web;</span></span></div></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="color: blue; font-family: 'Courier New'; font-size: x-small; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></span></div><div style="background-color: white; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: Verdana, sans-serif;">Now, if you can't see the Microsoft.Office.Server library in your usual reference store, don't panic. Go to your Twelve Hive folder and look for the ISAPI directory. The dll file should be in there. Add it in as a reference to your VB project.</span></div><div style="background-color: white; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 15px;"><span style="font-family: Verdana, sans-serif;"><br />
</span></span></div><div style="background-color: white; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: Verdana, sans-serif;">Let's presume you are going to create a conventional event handler for the ItemAdded event and copy the resulting data into a text field called "Business Unit". We'll skip the event handler code as we've covered that already. I'm now going to use a privilege wrapper and get the string value of the user field. This was the bit that caused me by far the most grief. The ToString() method would not work on the user control field. Don't ask me why it wouldn't work, it just wouldn't. Kept returning nulls. So I did it like this:</span><br />
<span style="font-family: Verdana, sans-serif;"><br />
</span><br />
<br />
<div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Guid</span> siteID = properties.SiteId;</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Guid</span> webID = properties.ListItem.Web.ID;</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Guid</span> listID = properties.ListId;</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Guid</span> listItemID = properties.ListItem.UniqueId;</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPSecurity</span>.RunWithElevatedPrivileges(<span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">delegate</span>()</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">{</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">using</span> (<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPSite</span> site = <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">new</span> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPSite</span>(siteID))</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> {</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">using</span> (<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPWeb</span> web = site.OpenWeb(webID))</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> {</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> </span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPList</span> list = web.Lists[listID];</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPListItem</span> item = list.Items[listItemID];</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">string</span> userValue = (<span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">string</span>)item[<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"Reported By User"</span>];</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPFieldUserValue</span> fieldUserValue = <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">new</span><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> </span><span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPFieldUserValue</span>(web, userValue);</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> </span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPUser</span> user = fieldUserValue.User;</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">string</span> department = GetBusinessUnit(user, site);</span><span style="background-color: white; font-family: 'Courier New'; line-height: 1.2em;"> </span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> item[<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"Business Unit"</span>] = department;</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> item.Update();</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> }</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> }</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> });</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">//...</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">//subroutine to get profile info here</span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"></span><br />
<div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">private</span> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">string</span> GetBusinessUnit(<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPUser</span> user, <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPSite</span> site)</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">{</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">string</span> busUnit = <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">string</span>.Empty;</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">try</span></span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> {</span><span style="background-color: white; font-family: 'Courier New'; line-height: 1.2em;"> </span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">ServerContext</span> serverContext = <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">ServerContext</span>.GetContext(site);</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">UserProfileManager</span> upm = <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">new</span> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">UserProfileManager</span>(serverContext);</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">UserProfile</span> profile = upm.GetUserProfile(user.LoginName);</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> busUnit = profile[<span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">"Department"</span>].ToString();</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> }</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">catch</span> (<span style="color: #2b91af; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Exception</span> ex)</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> {</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> //add your own error handling here</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> busUnit = ex.Message + <span style="color: #a31515; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">" : "</span> + errorArgs;</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> }</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> </span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> return</span> busUnit;</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></span></div><div style="font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">}</span></span></div><div style="font-family: Arial, sans-serif; font-size: medium; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New'; font-size: x-small; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></span></div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: Verdana, sans-serif;">This I can guarantee will work because I ran it successfully myself. One note with error trapping: if you create the profile and use a field that has a null value in the user's mySite, there's nothing here to gracefully trap that at present, unless you don't mind your control displaying a honking big error message :) </span></span></div></div></div>susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-89196926622097540402011-12-21T13:43:00.000-08:002011-12-21T13:43:03.572-08:00The Straw That Broke the CAML's back: Manipulating Content Type field in a viewThis post will be brief. I wish my discovery had been so too!<div><br />
</div><div>Let's say you create an SPQuery object from the default view of a list. Then you deploy your query, putting the result into an SPItemCollection object. That done, you want to determine a certain sequence of events based on the content type of the specific item when looping through the collection.</div><div><br />
</div><div>So, trying various methods in vain:</div><div><br />
</div><div><span style="font-family: 'Courier New', Courier, monospace;">SPQuery query = new SPQuery()</span></div><div><span style="font-family: 'Courier New', Courier, monospace;">SPListItemCollection items = list.GetItems(query);</span></div><div><br />
</div><div><span style="font-family: 'Courier New', Courier, monospace;">foreach (SPListItem item in items)</span></div><div><span style="font-family: 'Courier New', Courier, monospace;">{</span></div><div><span style="font-family: 'Courier New', Courier, monospace;"> if (item.ContentType.Name == "This line will throw an error)</span></div><div><span style="font-family: 'Courier New', Courier, monospace;"> {</span></div><div><span style="font-family: 'Courier New', Courier, monospace;"> //this condition never gets hit</span></div><div><span style="font-family: 'Courier New', Courier, monospace;"> }</span></div><div><span style="font-family: 'Courier New', Courier, monospace;"> </span></div><div><span style="font-family: 'Courier New', Courier, monospace;"> if (item["ContentType"].ToString() == "So will this one")</span></div><div><span style="font-family: 'Courier New', Courier, monospace;"> {</span></div><div><span style="font-family: 'Courier New', Courier, monospace;"> //this one doesn't either</span></div><div><span style="font-family: 'Courier New', Courier, monospace;"> }</span></div><div><span style="font-family: 'Courier New', Courier, monospace;"> if (item.ContentType.ID == "you get the idea blah blah"</span></div><div><span style="font-family: 'Courier New', Courier, monospace;"> ...</span></div><div><span style="font-family: 'Courier New', Courier, monospace;"><br />
</span></div><div><span style="font-family: 'Courier New', Courier, monospace;">}</span></div><div><span style="font-family: 'Courier New', Courier, monospace;"><br />
</span></div><div>Kept getting "Cannot complete this action", "object not set to instance of an object", and when I tried Content Type ID some exception that had HRESULTS and big long hexy number and god knows what.</div><div><br />
</div><div>Then I thought - the view I ran the query from - I did display the content type on that - didn't I? I went in and ticked the ContentType field in the view so that it was visible?</div><div><br />
</div><div>Nope. I didn't. So it couldn't find it. D'oh! I hope reading this saves you the trouble I got into!</div>susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-63331725214803697782011-11-15T10:16:00.001-08:002011-11-15T10:21:10.279-08:00Creating and Installing a SharePoint Timer Job<div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><b style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Important: this should be carried out on a non-production server first and then refined before deploying to live!</b></span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><b style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</b></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">I just created a timer scheduled job in SharePoint with the help of Some Code Off The Internet (TM) plus a few hard-earned lessons all of my own.</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">The important thing to realise about a timer job is that it's just another SharePoint feature plus dll with some feature overriding code in it. Nothing more, nothing less. When I download a project with all the components and setup stuff, I tend to just work with the actual .cs files in a class library, make the dll and then do it manually. That means I know what's going on.</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">The code I downloaded was here</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><u style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><a href="http://www.fsmpi.uni-bayreuth.de/~dun3/archives/visual-studio-2005-project-template-for-sharepoint-2007-timer-jobs/142.html" rel="nofollow" style="color: #003399; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" target="_blank">http://www.fsmpi.uni-bayreuth.de/~dun3/archives/visual-studio-2005-project-template-for-sharepoint-2007-timer-jobs/142.html</a></u></span></span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">(thank you Alexander Brütt)</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">But what I did rather than open the whole package as a Visual Studio item was to open the whole thing and drop all the build instruction files because I was deploying to another machine anyway. The three files you really need are</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Job.cs</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">JobInstaller.cs</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Feature.xml (which you will put somewhere separate in the FEATURES folder in the Twelve Hive anyway)</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">The rest is just detail. But don't forget the strong key. It saves time since that's the key in your feature.xml file. Also there are some items in the folder with dollar signs on them. They're for you to change the name. I called the Job class CustomSharePointJob and the Installer class CustomSharePointJobInstaller. Yes I was looking for an easy life there :)</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Build the thing as a dll (forgetting the manifest xml and all that packaging stuff) and if there are build errors, chances are you need to conjure up a couple of GUIDs to identify the dll as the code has $guid somewhere, I think. It should eventually build ok.</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">OK, then GAC register your dll and make sure the info in the feature.xml file matches up. The Feature Receiver line should be the same as your components. Stick it into the Twelve Hive. Now fire up the command line, change dir to your twelve hive BIN folder and enter the following piece of sublime poetry:</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">stsadm -o installfeature -filename CustomSharePointJob\Feature.xml</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">And then</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">stsadm -o activatefeature -filename CustomSharePointJob\Feature.xml -url <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><u style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><a href="http://mylittleserver/" rel="nofollow" style="color: #003399; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" target="_blank">http://mylittleserver</a></u></span></span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Hopefully it should give you the thumbs up. But just to make sure, open up SharePoint Central Admin and navigate to Operations - Timer Job Definitions. Your timer job should be there and its schedule should be Minutes.</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">OK, we want to deactivate it for a moment and do some work on it. So in stsadm</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">stsadm -o deactivatefeature -filename CustomSharePointJob\Feature.xml -url <span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><u style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><a href="http://mylittleserver/" rel="nofollow" style="color: #003399; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" target="_blank">http://mylittleserver</a></u></span></span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">The wonderful thing about this code is that it has event receivers for the feature deactivating, and the event triggers a delete. So you don't have to worry about taking the job off the list in sharepoint central admin, it's all taken care of.</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Now in Visual Studio, have a look at the Job.cs folder. I put in instructions to send me an email when the job fired - just to make sure it worked ok. In order to do that I had to instantiate an SPSite object and SPWeb object which I did as per usual using a site url. But there is one important difference to note. The code here specifies a job lock type of SPJobLockType.ContentDatabase. This means that the job is designed to fire against every content database on that server. I was wondering why I was getting nine emails! Go into your Job.cs file and change that to SPJobLockType.Job and you are sorted, it will only fire the once.</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Also, what else do I need to do. Well I don't want the damn thing running every 2 minutes, once a day is enough. I scoured the internet looking for something telling me how to set up a daily schedule and eventually found a solution which I've amended slightly to run at 6am. If, for testing purposes, you want to change that hour, just move up the BeginHour and EndHour properties:</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="color: #2b91af; font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">SPDailySchedule<span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> schedule = </span><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">new</span><span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"> </span>SPDailySchedule<span style="color: black; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">();</span></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">schedule.BeginHour = 6;</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">schedule.BeginMinute = 15;</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">schedule.BeginSecond = 0;</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">schedule.EndSecond = 15;</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">schedule.EndMinute = 15;</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">schedule.EndHour = 6;</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">myCustomSharePointJob.Schedule = schedule;</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">myCustomSharePointJob.Update();</span><br />
<span style="font-family: 'Courier New'; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">I put in some code to do the thing I wanted to do on the site I wanted to do it on - this goes in the Execute method - recompiled the dll and GAC registered it once more. Ready to do battle - but wait -</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><b style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Now this next step is very important</b></span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><b style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</b></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Save yourselves a good few hours of pain and do this now. Just when you've finished running gacutil from the command line, enter the following line:</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New', monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">net stop sptimerv3</span></span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New', monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">(it will be sptimerv4 for you sharepoint 2010 heads)</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">And then straight afterwards type</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New', monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">net start sptimerv3</span></span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="font-family: 'Courier New', monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">This stops and restarts the OWSTIMER.EXE process for sharepoint. (This is why not to do this on live.) The reason this has to be done is otherwise, the process will cache your old dll FOREVER. It doesn't matter if you re-register it seven times, doesn't matter if you delete the damn thing or restart the job or whatever - it will just keep caching it.</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><b style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">This has to be done every time the dll is recompiled!</b></span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><b style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</b></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Then - and only then - reactivate the feature by going into stsadm and entering the activatefeature command as described above. Then hopefully all should be well.</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">I would like to recommend the following links which are very helpful:</span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><u style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><a href="http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx" rel="nofollow" style="color: #003399; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" target="_blank">http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx</a></u></span></span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><u style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><a href="http://sharepoint.stackexchange.com/questions/17583/custom-timer-job-throws-an-error-value-cannot-be-null-parameter-name-uristr" rel="nofollow" style="color: #003399; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" target="_blank">http://sharepoint.stackexchange.com/questions/17583/custom-timer-job-throws-an-error-value-cannot-be-null-parameter-name-uristr</a></u></span></span><br />
<span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><a href="http://mssharepointtips.com/tip.asp?id=1021" rel="nofollow" style="color: #003399; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" target="_blank"><span style="color: blue; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><u style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">http://mssharepointtips.com/tip.asp?id=1021</u></span></a></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div>susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-20689714035682323542011-11-10T05:13:00.000-08:002011-11-10T05:16:13.999-08:00QueryOverride and its quirks - use with caution!<div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">This post deals with the quirks involved with the QueryOverride property in a Content Query Editor WebPart. Suffice it to say when they say "override" they mean it. It overrides almost everything you set for a Content Query Editor Web Part in your UI and if you are unprepared this is a massive PITA to find and fix. I was, naturally, unprepared and had to find and fix it.</span><br />
<span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">It started with a request to filter an existing UI so that news items lingering longer than a month would not be displayed. A bit of digging and I soon learned that the "Modify Shared Web Part" in the UI cannot calculate back from [Today], only a fixed date, so I would have to select Export on the menu and save to my own hard drive to edit the CAML in notepad. Naïve as I was, I thought it would be simple enough to replace the QueryOverride line with:</span><br />
<span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New', Courier, monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><property name="QueryOverride" type="string" ><![CDATA[<Where><And><Geq><FieldRef Name='Modified'/><Value Type="DateTime"><Today OffsetDays="-30"/></Value></Geq></Where>]]></property></span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">OK. Well I uploaded it to the site collection web part gallery, recreated my web part on the page to point to it, and that seemed to work. Last April's stuff was no longer appearing on the list. Except that one of my colleagues mentioned, "Hey, the order looks funny". I had a look and indeed she was right. Instead of descending order, it went from earliest first. So I went "Edit Page" and modified the web part to switch from Ascending to Descending, clicked "Apply" and hey presto…it switched right back to "Ascending" again! I tried this several times, with the same result. More googling and nail biting. Eventually I found that I needed to specify not only WHAT I wanted back, but what order I wanted it in. So my QueryOverride line became:</span><br />
<span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New', Courier, monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><property name="QueryOverride" type="string" ><![CDATA[<Where><Geq><FieldRef Name='Modified'/><Value Type="DateTime"><Today OffsetDays="-30"/></Value></Geq</Where><OrderBy><FieldRef Name="Created" Nullable="True" Type="DateTime" Ascending="FALSE"/></OrderBy>]]></property></span><br />
<span style="font-family: 'Courier New', Courier, monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">RIGHT. Upload, delete web part, recreate. So now we had the correct items appearing in the correct order. Happy Days. Except then the user said, "Hey, we have another problem".</span><br />
<span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">The CEWP I was editing was pulling all items from all site collections on the portal that had a particular page content type. One library had items which were not to be included in the main list. It had a different page content type. Everyone knew about this other page content type and used it. The main news webpart had always ignored it because it was pointing to a different content type. Everything had been fine. Up till the time I'd put in my QueryOverride property, when it had all gone to dog doodoo. The items from the not-to-be-included library were very much included, and what was I going to do about it, eh?</span><br />
<span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">I looked at the CAML of the two web parts, the good one and the bad one (i.e. mine) - I copied them both into Excel and eyeballed them there, for God's sakes - and could not see what the problem was. Both web parts had their respective content types clearly specified in the Presentation section. There could be no ambiguity. So what was going on?</span><br />
<span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">I finally discovered that QueryOverride doesn't care what you put in your content types. Once I put in the "Modified" date in the queryoverride, I was toast. It just hoovered up everything from everywhere REGARDLESS of what I specified for content types in the UI and what was written in the CAML. I realised I would have to tell the bloody thing for once and for all - don't use that &*(^ing content type. So now here is the final all-singing, all-dancing QueryOverride line I had to use. Oh and note the location of the <And> tags. I stuffed that up the first time and wondered why the logic wasn't working...</span><br />
<span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New', Courier, monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><property name="QueryOverride" type="string" ><![CDATA[<Where><And><Geq><FieldRef Name='Modified'/><Value Type="DateTime"><Today OffsetDays="-30"/></Value></Geq><Neq><FieldRef Name="ContentType"/><Value Type="Text">[My Little Content Type]</Value></Neq></And></Where><OrderBy><FieldRef Name="Created" Nullable="True" Type="DateTime" Ascending="FALSE"/></OrderBy>]]></property></span><br />
<span style="font-family: 'Courier New', Courier, monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Please note that I have put the content type name in square brackets. I don't think my employer would care for me to put in the real content type name!</span><br />
<span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">All right, good to go at last, surely! But no, not yet. The news items weren't displaying the way they were in the last web part, where we'd seen a nice little teaser under the headline. So I stuck the thing into Excel (again) to compare and amended the following CAML parameters:</span><br />
<span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span></div><div style="background-color: white; font-family: Arial, sans-serif; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New', Courier, monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><property name="ParameterBindings" type="string" null="true" /></span></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New', Courier, monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><property name="CommonViewFields" type="string">PublishingPageContent</property></span></div><div style="background-color: white; line-height: 19px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; outline-color: initial; outline-style: none; outline-width: initial; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><span style="font-family: 'Courier New', Courier, monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><property name="NoDefaultStyle" type="string" null="true" /></span><br />
<span style="font-family: 'Courier New', Courier, monospace; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;"><br />
</span><br />
<span style="font-family: inherit; line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;">Finally! It worked!</span></div>susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com6tag:blogger.com,1999:blog-7589311256418017787.post-70262268942089813712011-10-27T13:45:00.000-07:002011-10-27T13:48:33.994-07:00The All-Singing, All-Dancing Site Collection Recursion Code!Need to update some setting in every site in your site collection? Hunting around the internet googling "Recursion", "Site Collection" and "SharePoint"? Here I am to your rescue. Create a console application, add in a ref to Sharepoint and replace the one line in Main() with your site collection. Stick your own code into the bit where it says "Put your code here", run, and Bob's your uncle, auntie and whatever sort of relative you're having yourself. Can be adapted for web part too if you want. The streamwriter dumps a load of updates into your chosen folder.<br />
<br />
Why, you're welcome :)<br />
<br />
<span class="Apple-style-span" style="background-color: white; font-family: monospace; font-size: 14px; line-height: 16px;">//Code by Susan Lanigan, 25/10/2011<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" />using System;<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" />using System.Collections.Generic;<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" />using System.Text;<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" />using Microsoft.SharePoint;<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" />using System.IO;<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" />namespace RecursiveConsoleApp<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" />{<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> class Program<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> {<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> static void Main(string[] args)<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> {<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> //call once and let recursion take care of the rest<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> //start from a site collection top level URL<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> //NOTE - if you want to break completely on an error<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> //stick a try-catch block up here and get the sub to throw;<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> DoTheWork("http://myServer/mySiteColl", string.Empty);<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> }<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> static void DoTheWork(string siteUrl, string parentWebName, string eMail)<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> {<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> string errorArgs = string.Empty;<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> try<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> {<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> using (SPSite site = new SPSite(siteUrl))<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> {<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> using (SPWeb web = site.OpenWeb())<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> {<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> // another try-catch for the filestreamer<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> //write console output to file<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> FileStream fs = new FileStream("c:\\Susan\\LogForSite_" + parentWebName + "_" + web.Name + ".txt", FileMode.Create);<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> StreamWriter sw = new StreamWriter(fs);<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> TextWriter tmp = Console.Out;<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> try<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> {<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> Console.SetOut(sw);<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> Console.WriteLine("Writing update for site: " + web.Url);<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> //DO YOUR WORK HERE AND WRITE TO STREAMWRITER ETC.<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> //...</span><br />
<span class="Apple-style-span" style="background-color: white; font-family: monospace; font-size: 14px; line-height: 16px;"><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> }<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> catch (Exception fileEx)<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> {<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> Console.WriteLine(fileEx.Message);<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> return;<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> }<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> finally<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> {<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> sw.Close();<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> fs.Close();<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> }<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> //reset console output<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> Console.SetOut(tmp);<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> //now for the subsites<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> SPWebCollection subSites = web.Webs;<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> foreach (SPWeb subSite in subSites)<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> {<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> //RECURSION HERE<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> DoTheWork(subSite.Url, web.Name, eMail);<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> subSite.Close();<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> }<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> }<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> }<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> }<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> catch (Exception ex)<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> {<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> //custom error handling stuff<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> Console.WriteLine(ex.Message);<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> Console.ReadLine();<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> <br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> return;<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> }<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> }<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /> }<br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" /><br style="line-height: 1.2em; outline-color: initial; outline-style: none; outline-width: initial;" />}</span>susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-16702094250761515292011-10-22T04:48:00.000-07:002011-10-22T04:48:03.844-07:00Copying File from one Doc Library to Another - Problem SolvedI forgot to update about this. I solved that problem that had me removing several hairs at the follicle.<br />
<br />
In order to copy the file from one place to the other, I had to create a using block for the source site and then the target site. Thus (note that this was in a separate class file from the event handler so I did not have access to the properties object and had to pass the IDs in as parameters):<br />
<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">public static void CopyFile (Guid listID, int itemID, string sourceUrl, string targetUrl, Guid targetListID)</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">{</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> try</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> { </span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> using (SPSite sourcesite = new SPSite(sourceUrl))</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> {</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> using (SPWeb sourceWeb = sourceSite.OpenWeb())</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> {</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> SPList list = web.Lists[listID];</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> SPListItem item = list.Items[itemID];</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> SPFile file = item.File;</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> //Here was where my error started</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> //I instantiated the file out of scope</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> byte[] binFile = file.OpenBinary();</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> </span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> //OPEN TARGET SITE AND WEB HERE</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> using (SPSite targetSite = new SPSite(targetUrl))</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> {</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> using (SPWeb targetWeb = targetSite.OpenWeb())</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> {</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> //get the target list</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> //create SPFolder object</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> //this is like a SPList object</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> //but concerned only with the copying of files</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> folder.Files.Add(binFile);</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> }</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> }</span><br />
<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> </span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> }</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> }</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"> }</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">}</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: inherit;">My error appeared to be where I declared the binary array. When I declared it <i>within scope of the target site</i> everything appeared to work perfectly, even though I was just streaming the file on the other site like before. I have no idea why a binary stream should lose scope, it's only an array, but I could not make it work for a non-admin user.</span><br />
<span class="Apple-style-span" style="font-family: inherit;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: inherit;">Please note that I haven't put in the target file copying code in detail as I am working from memory here - but it needs to be done using the </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">SPFolder</span><span class="Apple-style-span" style="font-family: inherit;"> object. The SPFolder objects points to the document library just like the </span><span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">SPList </span><span class="Apple-style-span" style="font-family: inherit;">object, but only deals with the files. Because the SPList object only handles stuff on lists. It doesn't do files. Jobsworth of an object!</span>susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-76138166467756369552011-10-15T09:26:00.000-07:002011-10-22T04:48:57.335-07:00Extending and Overriding my own WitsWell I am making some progress on that non-event from hell detailed in my last post. First I commented out everything in my event handler and put in some innocuous line like:<br />
<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">properties.ListItem("Title") = "Changed by the event handler";</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">properties.ListItem.Update();</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: inherit;">And that worked. So my fears that the event handling code was not invoked at all when logging in as a non-admin, nothing special user were unfounded.</span><br />
<span class="Apple-style-span" style="font-family: inherit;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: inherit;">So, I commented back in the error handling code. Nothing happened. OK, so the error handler doesn't work. At least that explains why I wasn't getting any feedback. Strange given that the test user had access to the folders but not crucial but will worry about it later.</span><br />
<span class="Apple-style-span" style="font-family: inherit;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: inherit;">So what did I do then? Moved the comment /* indicator down a few painstaking lines each time, compiled, GACed, IISResetted and ran. Used the Title field as my error handler! Bit by bit by bit. The code had no problem calling the web service or using the impersonator token. What it did not want to do was copy the binarily streamed file (is that valid syntax? Probably not, but if the method of streaming a file is binary, then surely it is binarily streamed, no? Anyway.) It streamed it fine, even modified as Mr High-Up Impersonated User just as I asked it to with the SPUserToken. But it won't copy it.</span><br />
<span class="Apple-style-span" style="font-family: inherit;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: inherit;">I suspect I will have to put a Special Snowflake sodding privilege block around that operation all by itself or else it will sulk forever. So, will I succeed. Tune in...to be continued in our next. If you haven't died of boredom first...</span>susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-65046746003314139682011-10-13T13:23:00.000-07:002011-10-22T04:52:21.929-07:00At My Wit's EndI cannot get an event handler to fire at all for a non-admin user.<br />
<br />
I have error tracking enabled and on. It writes to a file. I have enabled permissions on the file folder. I have instructed it to throw an error and write back. When I log in as myself, it does all these things. I am a member of the owner group of the site.<br />
<br />
The event handler copies the document library entry, including the document, to another document library on a different site, in a different site collection. It does various other things as well, but that's later on. When I log in as myself, all this works fine. When I log in as the test user, nothing happens. When I instrument the code, nothing happens. I have tried elevated privilege blocks in the code, paying attention to the declaration of the site instances <i>within</i> the block as required, but given that the event handling mechanism steadfastly refuses to even CALL the code for that user, I'm inclined to believe I could put "rhubarb rhubarb rhubarb" in there and it wouldn't make any difference. There should be no reason why I cannot manipulate the properties object.<br />
<br />
I made the test user King of Everything. Full control on the source document library, full control on the site - I even made the blasted thing a site collection administrator! (this on the test machine, of course) Test user had more privileges than I did. Plus I removed my login from the site collection admin list.<br />
<br />
Didn't make a lick of difference. And then when an actual user tried to log in and nothing happened, I knew this was going to be trouble. I have to confess to being utterly stumped as to why it doesn't call the code AT ALL. I would understand if it gave an error due to privilege - I even managed to get past one of those in one of the other issues I had with the EnsureUser method - but it's not even bothering to invoke the component. Works fine when I log in as myself, or log in as super user.<br />
<br />
Any ideas? Losing the will to live over here! :)<br />
<br />
Susansusanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com2tag:blogger.com,1999:blog-7589311256418017787.post-52817124660308770462011-10-11T00:47:00.000-07:002011-10-22T04:56:47.462-07:00RunWithElevatedPrivileges - another encounter and something you should knowI have been previously complaining about SPSecurity.RunWithElevatedPrivileges as being a load of tosh. I might have been a tad harsh as I employed it just yesterday and got it working - almost.<br />
<br />
I needed to do two things - copy over a file and update fields. The file copy bit worked well - I had an impersonation block for that - but the bit where a non-admin user updates the fields was, alas, a bit of a dud. Until I put the field update section into a RunWithElevatedPrivilege delegate block and all looked fine.<br />
<br />
But in addition to the native data types I was using, I also had four user fields I was obtaining from a web service off somewhere else. In order to update these, I had to get the username and call the EnsureUser method on my elevated privileges SPWeb object. This was still failing, even within the privileged user wrapper. It either said I couldn't find the user or that I wasn't allowed to get it. This even though I had, as Google's wisdom advised, wrapped the EnsureUser method call with AllowUnsafeUpdates = true on either side.<br />
<br />
Then I commented out the web.AllowUnsafeUpdates = true line and its corresponding AllowUnsafeUpdates=false on the other side. Ran it again. Worked perfectly. I had been trying this for days!<br />
<br />
So, you heard it here first - when using the delegate, try not setting the AllowUnsafeUpdates property. And definitely don't set it on the Site object or the whole thing will break completely. SharePoint seems perfectly capable of deciding for itself, in this case, whether the update is safe or not.susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0tag:blogger.com,1999:blog-7589311256418017787.post-28908883237514399102011-10-03T12:43:00.000-07:002013-06-12T12:26:00.772-07:00Programmatically Copying a Recurring Event From One Calendar to Another<span class="Apple-style-span" style="font-family: inherit;">I have been spending the last while trying to figure out a problem I had with an event handler I had configured for a calendar. The idea is that if you add an event to a calendar where the event handler is switched on, it bubbles up to a parent calendar which contains all the events. There can be many sub-calendars, only one main one.</span><br />
<div>
<span class="Apple-style-span" style="font-family: inherit;"><br />
</span></div>
<div>
<span class="Apple-style-span" style="font-family: inherit;">So, I created my event handler just as I have described in previous entries. Create an event on the child, bubbles up to the parent. Create a common key between the two. Add an ItemUpdated event handler so then if the child event is updated the parent is updated too. Same with ItemDeleted. Everything hunky dory -</span></div>
<div>
<span class="Apple-style-span" style="font-family: inherit;"><br />
</span></div>
<div>
<span class="Apple-style-span" style="font-family: inherit;">OK, wait, back up the truck. What about recurring events? How do they work?</span></div>
<div>
<span class="Apple-style-span" style="font-family: inherit;"><br />
</span></div>
<div>
<span class="Apple-style-span" style="font-family: inherit;">Leaving them out was not an option. The department had quite a few weekly meeting events. So I hit Mr Google and <a href="http://msdn.microsoft.com/en-us/library/ms434156.aspx">got a handy link from MSDN here</a> which suggested the following code.</span></div>
<div>
<br /></div>
<div>
<pre style="font-family: Consolas, Courier, monospace; font-size: 13px; font-style: normal; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; word-break: break-all; word-wrap: break-word;">SPListItem recEvent = listItems.Add();
<span style="color: blue;">string</span> recData = <span style="color: #a31515;">"<recurrence><rule>"</span> +
<span style="color: #a31515;">"<firstDayOfWeek>su</firstDayOfWeek>"</span> +
<span style="color: #a31515;">"<repeat><daily dayFrequency='1' /></repeat>"</span> +
<span style="color: #a31515;">"<repeatInstances>5</repeatInstances></rule></recurrence>"</span>;
recEvent[<span style="color: #a31515;">"Title"</span>] = evtTitle;
recEvent[<span style="color: #a31515;">"RecurrenceData"</span>] = recData;
recEvent[<span style="color: #a31515;">"EventType"</span>] = 1;
recEvent[<span style="color: #a31515;">"EventDate"</span>] = <span style="color: blue;">new</span> DateTime(2011,8,15,8,0,0);
recEvent[<span style="color: #a31515;">"EndDate"</span>] = <span style="color: blue;">new</span> DateTime(2011,9,25,9,0,0);
recEvent[<span style="color: #a31515;">"UID"</span>] = System.Guid.NewGuid();
recEvent[<span style="color: #a31515;">"TimeZone"</span>] = 13;
recEvent[<span style="color: #a31515;">"Recurrence"</span>] = -1;</pre>
<pre style="font-family: Consolas, Courier, monospace; font-size: 13px; font-style: normal; overflow-x: auto; overflow-y: auto; padding-bottom: 5px; padding-left: 5px; padding-right: 5px; padding-top: 5px; word-break: break-all; word-wrap: break-word;">recEvent.Update();</pre>
</div>
Now obviously I need to change the line where they build the RecurrenceData property because this will depend on whatever that is for the original object. So the line to use will be<br />
<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">item["RecurrenceData"] = properties.ListItem["RecurrenceData"];</span><br />
<br />
and for the same reason I don't need to code the UID. So I save it and all is well. Then I a dd the recurring event. Let's say I set the start time at 1pm, the end time at 2pm, set it to occur weekly on a Tuesday for two weeks, and save it. Then I go to my parent calendar. In spite of the MSDN advice, still no sign of the recurring event being copied over.<br />
<br />
So, I turn on the error handler and get back this error: Value does not fall within accepted range. This error means that the field RecurrenceData does not exist on the list. But how can it not exist? The Recurrence field exists for every event, surely?<br />
<br />
Ah - but they are only visible on the form if you click the little Recurring Event checkbox! So if they are not visible on the form, some little quirk means that they are not visible in the code. This is probably because one recurring event contains many child events. So after much searching around<a href="http://social.msdn.microsoft.com/Forums/eu/sharepointdevelopment/thread/6b9a0442-caaf-48b5-83c1-b7cc83c79236"> I found this link </a>which explained that I had to set the ShowInEdit property of the field concerned to true:<br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"><br />
</span><br />
<span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">item.Fields["Recurrence"].ShowInEditForm = true;</span><br />
<br />
The only thing that did not work for me in that link was the use of the <span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">GetInternalFieldName</span> method. I replaced that with the <span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">GetField</span> method, used those lines of code before updating each recurrence data field - you need to do it for all of them and don't forget to call <span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;">this.DisableEventFiring()</span> after each update! - and then built it once more. Hey presto, it created an event - of sorts.<br />
<br />
The problem I have now is that the event that gets created on the parent calendar is a long, unspecified blob that covers all the days of the recurrence time. In order to have it work properly, I had to go into the event from the UI and click "Edit Item". The recurrence info is all there, present and correct. Click Ok, click past the warning message, and it's fine. All I need to do is mimic that update in code. All ideas and suggestions welcome!<br />
<br />
ETA. See the following solution kindly provided by Noelle Marchbanks<br />
<br />
<span style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;">Add to your copy code:</span><br style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;" /><span style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;">recEvent["EventType"] = properties.ListItem["EventType"];</span><br style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;" /><span style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;">recEvent["UID"] = System.Guid.NewGuid();</span><br style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;" /><br style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;" /><span style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;">Even though you are copying an event, these fields are necessary to make it realize that a Recurrence is appropriate. It is IMPERATIVE that you set the EventType to the same as the copied list item, and not hard-code it as 1 as it will break non-recurring events and create orphaned exceptions to the rule.</span><br style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;" /><br style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;" /><span style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;">If, for some reason, you do get orphaned recurring event children (e.g. ID = 1.1.x instead of ID = 1), open up powershell, get the item as an object, set its EventType to 1, then delete it from the list.</span><br style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;" /><br style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;" /><span style="background-color: white; font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 16px; text-align: justify;">Hope this helps.</span>susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com6tag:blogger.com,1999:blog-7589311256418017787.post-81238843002819644892011-09-27T12:39:00.000-07:002011-09-27T12:39:12.067-07:00SharePoint Designer 2007 Workflows Are Not Portable. At All. No Matter What It Says.SharePoint Designer lied to me last week. I had a SharePoint list on a site and the user of the site wanted to rename the library upon which I had compiled the workflow. In Designer, you have the ability to create workflow on a list - a bit like event handlers but without code involved - and I went in to have a look at the one I'd just made. On the first screen, where it says the List Name, it appeared that it had changed the name in the workflow just as the user had on the site. I opened it and clicked Finish to re-run it. No errors. Happy days, everything was working fine.<br />
<br />
Until I got an email saying it wasn't working. Cue much embarrassment on my part, apologies, and then the one thing I had thought I might avoid - deleting the workflow and recreating it all over again. But really the take home message from this experience is this:<br />
<br />
<b>Whenever you need to move or alter a workflow in Sharepoint Designer 2007 because of a change to the site or list, you need to recreate it again from scratch.</b><br />
<b><br />
</b><br />
This is because Sharepoint Designer retains a hard-coded memory of those blasted GUIDs. You can never use the workflow for any other list, you can't move it anywhere, and as I have just learned, you can't rename the list. Oh and forget trying to wrap it up in a template file in .stp format - it won't work.<br />
<br />
And it won't bother giving any errors. Errors are for wimps.<br />
<br />
The second take-home message would be - Whenever you regenerate the workflow, create a new item on the new list. Yes, even if the list is "live". You can always delete it. Just don't trust Sharepoint Designer 2007 to tell you anything!susanlhttp://www.blogger.com/profile/04467608503060142476noreply@blogger.com0