Donnerstag, 18. April 2013

Setting the Dataprovider for Resultscriptwebpart programmatically (eg. for setting the QueryTemplate)

For doing that you have to create a instance of the DataProviderScriptWebpart, initialize it with the JsonData from the ResultScriptWebPart. There you can set the Propertiy you need and write the PropertiesJason-Member back to the ResultScriptWebpart.DataProviderJson. And of course save the Webpart finally.


           var resultWebPart =  
             webPartManager.WebParts.Cast<WebPart>()  
                    .FirstOrDefault(  
                      wp => wp is ResultScriptWebPart) as ResultScriptWebPart;  
   
           var querySettings = new DataProviderScriptWebPart  
             {  
               PropertiesJson =  
                 resultWebPart.DataProviderJSON  
             };  
   
           querySettings.Properties["QueryTemplate"] =  
             "{searchboxquery} (contentclass:STS_ListItem OR IsDocument:True)";  
   
           resultWebPart.DataProviderJSON = querySettings.PropertiesJson;  
   
           webPartManager.SaveChanges(resultWebPart);  

But beware: Altough there is a Property DataProviderScriptWebPart.QueryTemplate which you can set, it is not included if you request the DataProviderScriptWebPart.PropertiesJson. You have to set the property using the  Properties-Collection of the object.

(of course you could also simple edit the Json-String itself, but I prefer this way).

Sonntag, 14. April 2013

Filtering a List by Userfield and Username

I recently had an requirement to get all Items of a List for a specific user. Usually that´s not that problem, using Server-side Code you could simply get the users SPUser-Object by SPWeb.EnsureUser and filter the list based on the SPUsers´ ID. However, I had to do that by using the Client Object Model (in detail, the Javascript API). Here the process of doing an async request to get the SPUser-Object and then do another async Request filtering by its ID didn´t make me happy at all, so I thought of another way doing that.

I found a solution by joining the List with the Hidden Userinfo-List, on that Join I was able to do a filter on the username.

Here´s the View-XML for doing that:

 <View>  
      <Joins>  
           <Join Type='INNER' ListAlias='User Information List'>  
                <Eq>  
                     <FieldRef Name='UserField' RefType='Id' />  
                     <FieldRef List='User Information List' Name='ID' />  
                </Eq>  
           </Join>  
      </Joins>  
      <Query>  
           <Where>  
                <Eq>  
                     <FieldRef Name='MyUserName' />  
                     <Value Type='Text'>Domain\UserName</Value>  
                </Eq>  
           </Where>  
      </Query>  
      <ProjectedFields>  
           <Field Name='MyUserName' Type='Lookup' List='User Information List' ShowField='Name' />  
      </ProjectedFields>  
 </View>  

(Beware: You have to do a double-Backslash for between Domain and Username when setting the XML-String to the query, because it removes one of them)

Additionaly you can also set ViewFields, for minimizing the result for faster response